ImageView не является центром CardView

У меня есть квадратное изображение изображения, и мой ImageView в моем CardView также квадратный, но он показывает мне такое изображение: ImageView не является центром CardView

Исходное изображение:

ImageView не является центром CardView

Это мой файл макета:

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.design.card.MaterialCardView xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:app = "http://schemas.android.com/apk/res-auto"
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    app:cardBackgroundColor = "@android:color/white"
    app:cardElevation = "2dp"
    app:cardPreventCornerOverlap = "true"
    android:layout_margin = "10dp">

    <LinearLayout
        android:id = "@+id/linearLayout"
        android:layout_width = "374dp"
        android:layout_height = "match_parent"
        android:orientation = "vertical">

        <ImageView
            android:id = "@+id/illness_image"
            android:layout_width = "364dp"
            android:layout_height = "364dp"
            android:background = "?attr/colorPrimaryDark"
            android:cropToPadding = "true" />

        <LinearLayout
            android:layout_width = "match_parent"
            android:layout_height = "wrap_content"
            android:orientation = "vertical"
            android:padding = "16dp">

            <TextView
                android:id = "@+id/illness_title"
                android:layout_width = "match_parent"
                android:layout_height = "wrap_content"
                android:text = "Title"
                android:textAppearance = "?attr/textAppearanceHeadline6"
                android:textSize = "20sp" />

            <TextView
                android:id = "@+id/illness_description"
                android:layout_width = "match_parent"
                android:layout_height = "wrap_content"
                android:text = "Description"
                android:textAppearance = "?attr/textAppearanceBody2"
                android:textSize = "10sp" />

            <TextView
                android:id = "@+id/illness_engagement"
                android:layout_width = "match_parent"
                android:layout_height = "wrap_content"
                android:gravity = "right"
                android:text = "Engagment"
                android:textAppearance = "?attr/textAppearanceBody2"
                android:textSize = "10sp" />
        </LinearLayout>
    </LinearLayout>
</android.support.design.card.MaterialCardView>

Обновлено: Извините, но я не понимаю. Я не могу вставить свой XML-код.

Показать файл xml ..

Piyush 18.12.2018 11:00

опубликуйте свой файл карты и макета

M D 18.12.2018 11:00

@Oliver Weidner, покажите свое свойство ImageView на xml или добавьте код ImageView в этот вопрос.

ॐ Rakesh Kumar 18.12.2018 11:01

Извините, у меня возникли проблемы с его вставкой. Теперь он там.

Oliver Weidner 18.12.2018 11:01

Удалите этот тег android:cropToPadding = "true" и используйте android:scaleType = "fitXY" на ImageView.

ॐ Rakesh Kumar 18.12.2018 11:03

@ Оливер Вайднер, ваша проблема исправлена?

ॐ Rakesh Kumar 18.12.2018 11:11

@ Ракеш Кумар нет, это не сработало

Oliver Weidner 18.12.2018 11:24

@OliverWeidner, в чем проблема?

ॐ Rakesh Kumar 18.12.2018 11:35

Результат был таким же, как и раньше. Проверьте ответ Нилеша Ратода, он почти получил его, но еще не хватает одного «кусочка» картинки. Было бы здорово, если бы вы могли нам помочь

Oliver Weidner 18.12.2018 11:38
2
9
917
4
Перейти к ответу Данный вопрос помечен как решенный

Ответы 4

Ответ принят как подходящий

Просто добавьте android:layout_gravity = "center" в свой ImageView, это сработает

Попробуй это

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.design.card.MaterialCardView xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:app = "http://schemas.android.com/apk/res-auto"
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    android:layout_margin = "10dp"
    app:cardBackgroundColor = "@android:color/white"
    app:cardElevation = "2dp"
    app:cardPreventCornerOverlap = "true">

    <LinearLayout
        android:id = "@+id/linearLayout"
        android:layout_width = "match_parent"
        android:layout_height = "match_parent"
        android:orientation = "vertical">

        <ImageView
            android:id = "@+id/illness_image"
            android:layout_width = "350dp"
            android:layout_height = "364dp"
            android:layout_gravity = "center"
            android:scaleType = "centerInside"
            android:src = "@drawable/dishu" />

        <LinearLayout
            android:layout_width = "match_parent"
            android:layout_height = "wrap_content"
            android:orientation = "vertical"
            android:padding = "16dp">

            <TextView
                android:id = "@+id/illness_title"
                android:layout_width = "match_parent"
                android:layout_height = "wrap_content"
                android:text = "Title"
                android:textAppearance = "?attr/textAppearanceHeadline6"
                android:textSize = "20sp" />

            <TextView
                android:id = "@+id/illness_description"
                android:layout_width = "match_parent"
                android:layout_height = "wrap_content"
                android:text = "Description"
                android:textAppearance = "?attr/textAppearanceBody2"
                android:textSize = "10sp" />

            <TextView
                android:id = "@+id/illness_engagement"
                android:layout_width = "match_parent"
                android:layout_height = "wrap_content"
                android:gravity = "right"
                android:text = "Engagment"
                android:textAppearance = "?attr/textAppearanceBody2"
                android:textSize = "10sp" />
        </LinearLayout>
    </LinearLayout>
</android.support.design.card.MaterialCardView>

Не работает, показывает мне ту же обрезанную картинку, но спасибо.

Oliver Weidner 18.12.2018 11:07

@OliverWeidner измените ширину LinearLayout вашего на android:layout_width = "match_parent"

AskNilesh 18.12.2018 11:10

Смотрится лучше, но цельной картины пока нет. www1.xup.in/exec/ximg.php?fid=16331743

Oliver Weidner 18.12.2018 11:16

@OliverWeidner, каков ваш ожидаемый результат ?? можешь поделиться? \

AskNilesh 18.12.2018 11:17

проверьте мой вопрос там я разместил свое исходное изображение

Oliver Weidner 18.12.2018 11:24

@OliverWeidner попробуйте с android:scaleType = "centerInside" в Imageview

AskNilesh 18.12.2018 11:28

Опять тот же результат .. я не понимаю, что здесь не так

Oliver Weidner 18.12.2018 11:30

Тот же результат снова

Oliver Weidner 18.12.2018 11:36

@OliverWeidner просто текст попробуйте уменьшить ширину вашего изображения, например android:layout_width = "200dp", я думаю, проблема с вашим изображением не в коде

AskNilesh 18.12.2018 11:37

@OliverWeidner, не могли бы вы показать нам, какие результаты вы получили после внесения вышеуказанных изменений?

ॐ Rakesh Kumar 18.12.2018 11:40

Ну вот, теперь его там! Большое вам спасибо!

Oliver Weidner 18.12.2018 11:40

Отлично, мы рады помочь вам и счастливого кодирования. :)

ॐ Rakesh Kumar 18.12.2018 11:41

Спасибо, желаю тебе того же! :)

Oliver Weidner 18.12.2018 11:42

Попробуй это

 <?xml version = "1.0" encoding = "utf-8"?>
 <android.support.design.card.MaterialCardView xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res-auto"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
app:cardBackgroundColor = "@android:color/white"
app:cardElevation = "2dp"
app:cardPreventCornerOverlap = "true"
android:layout_margin = "10dp">

<LinearLayout
    android:id = "@+id/linearLayout"
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    android:orientation = "vertical">

    <ImageView
        android:id = "@+id/illness_image"
        android:layout_width = "match_parent"
        android:layout_height = "364dp"
        android:background = "?attr/colorPrimaryDark"
        android:scaleType = "fitXY"
       />

    <LinearLayout
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:orientation = "vertical"
        android:padding = "16dp">

        <TextView
            android:id = "@+id/illness_title"
            android:layout_width = "match_parent"
            android:layout_height = "wrap_content"
            android:text = "Title"
            android:textAppearance = "?attr/textAppearanceHeadline6"
            android:textSize = "20sp" />

        <TextView
            android:id = "@+id/illness_description"
            android:layout_width = "match_parent"
            android:layout_height = "wrap_content"
            android:text = "Description"
            android:textAppearance = "?attr/textAppearanceBody2"
            android:textSize = "10sp" />

        <TextView
            android:id = "@+id/illness_engagement"
            android:layout_width = "match_parent"
            android:layout_height = "wrap_content"
            android:gravity = "right"
            android:text = "Engagment"
            android:textAppearance = "?attr/textAppearanceBody2"
            android:textSize = "10sp" />
    </LinearLayout>
</LinearLayout>

удалите android: cropToPadding, а затем попробуйте

Ajay Gohel 18.12.2018 11:11

Поместите этот LOC

        android:scaleType = "centerInside"

на ваш ImageView

<ImageView
            android:id = "@+id/illness_image"
            android:layout_width = "364dp"
            android:scaleType = "centerInside"
            android:layout_height = "364dp"
            android:background = "?attr/colorPrimaryDark"
            android:cropToPadding = "true" />

Вы можете использовать это следующим образом: -

<ImageView
       android:id = "@+id/illness_image"
       android:layout_width = "match_parent"
       android:layout_height = "wrap_content"
       android:scaleType = "fitXY"
       android:background = "?attr/colorPrimaryDark"
/>

Другие вопросы по теме