Неправильный просмотр карты в 5.1.1 Tablet и 5.0.2 Mobile Device


У меня есть следующее представление, созданное с помощью CardView .
Добавление зависимости compile 'com.android.support:cardview-v7:23.0.+' в зависимость gradle

Ниже приведен XML-файл для того же.

<android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/cardViewEmp"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="0.50"
                card_view:cardCornerRadius="20dp"
                card_view:cardElevation="10dp"
                android:padding="@dimen/margin_10"
                >
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    >
                <ImageView
                    android:id="@+id/employeeIcon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingTop="@dimen/margin_5"
                    android:src="@drawable/employeeicon"
                    android:layout_centerHorizontal="true"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Employee"
                    android:layout_centerHorizontal="true"
                    android:layout_below="@+id/employeeIcon"
                    android:textSize="@dimen/textSizeNormal"
                    />
                </RelativeLayout>
            </android.support.v7.widget.CardView>


            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/cardViewVehicle"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="0.50"
                card_view:cardCornerRadius="20dp"
                card_view:cardElevation="10dp"
                android:padding="@dimen/margin_10"
                >
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    >
                    <ImageView
                        android:id="@+id/vehicleIconLive"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingTop="@dimen/margin_5"
                        android:src="@drawable/vehicleicon"
                        android:layout_centerHorizontal="true"
                        />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Vehicle"
                        android:layout_centerHorizontal="true"
                        android:layout_below="@+id/vehicleIconLive"
                        android:textSize="@dimen/textSizeNormal"
                        />
                </RelativeLayout>
                </android.support.v7.widget.CardView>

Теперь высота и внешний вид представления в мобильной версии 4.4.4 выглядят так, как показано ниже, что выглядит хорошо!
введите здесь описание изображения

Но то же самое выглядит очень странно в 5.1.1 Nexus Tablet и 5.0.2 Mobile, как показано ниже.
введите здесь описание изображения

Я добавил высоту и угловой радиус этим

card_view:cardCornerRadius="20dp"
card_view:cardElevation="10dp"
  • Любой может ясно видеть, что угловой радиус выглядит странно без надлежащей высоты.
  • В чем может быть проблема.
  • Что можно сделать, чтобы CardView выглядел правильно?

Редактировать 1:
Пробовал с com.android.support:cardview-v7:23.1.1 , но результат снова был таким же.


person Sreehari    schedule 09.02.2016    source источник
comment
позвони compile 'com.android.support:cardview-v7:23.1.1'   -  person IntelliJ Amiya    schedule 09.02.2016
comment
@IntelliJAmiya Я пробовал то же самое. Не работает в планшете Nexus 5.1.1!!!   -  person Sreehari    schedule 09.02.2016


Ответы (3)


Попробуйте удалить: атрибут android:padding из представления карточки.

из документов:

Поскольку отступы используются для смещения содержимого для теней, вы не можете установить отступы в CardView. Вместо этого вы можете использовать атрибуты заполнения содержимого в XML или setContentPadding(int, int, int, int) в коде, чтобы установить заполнение между краями карты и дочерними элементами CardView.

Также это:

Обратите внимание, что если вы укажете точные размеры для CardView, из-за теней область его содержимого будет различаться между платформами до L и после L. Используя значения ресурсов, специфичные для версии API, вы можете избежать этих изменений. В качестве альтернативы, если вы хотите, чтобы CardView добавлял внутренние отступы на платформах L и более поздних, вы можете установить для setUseCompatPadding(boolean) значение true.

person Mina Wissa    schedule 09.02.2016

Наконец я разобрался с проблемой. Это произошло из-за внешнего LinearLayout, который я написал, чтобы сделать значки в центре экрана, высота которых была равна wrap_content. Я избегал LinearLayout и с помощью следующего кода сделал вид карты с правильными кривыми высоты и в середине.

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mainRL"
        >
<android.support.v4.widget.Space
                android:layout_width="@dimen/margin_10"
                android:layout_height="1dp"
                android:layout_centerInParent="true"
                android:layout_centerVertical="true"
                android:id="@+id/spaceCenter"
                />
<!-- Employee icon -->

            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/cardViewEmp"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                card_view:cardCornerRadius="20dp"
                card_view:cardElevation="10dp"
                android:layout_toLeftOf="@+id/spaceCenter"
                android:layout_centerInParent="true"
                >
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    >
                <ImageView
                    android:id="@+id/employeeIcon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingTop="@dimen/margin_5"
                    android:src="@drawable/employeeicon"
                    android:layout_centerHorizontal="true"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="  Employee  "
                    android:layout_centerHorizontal="true"
                    android:layout_below="@+id/employeeIcon"
                    android:textSize="@dimen/textSizeNormal"
                    />
                </RelativeLayout>
            </android.support.v7.widget.CardView>

            <!-- Vehicle icon -->

            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/cardViewVehicle"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                card_view:cardCornerRadius="20dp"
                card_view:cardElevation="10dp"
                android:layout_toRightOf="@+id/spaceCenter"
                android:layout_centerInParent="true"
                >
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    >
                    <ImageView
                        android:id="@+id/vehicleIconLive"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingTop="@dimen/margin_5"
                        android:src="@drawable/vehicleicon"
                        android:layout_centerHorizontal="true"
                        />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="    Vehicle     "
                        android:layout_centerHorizontal="true"
                        android:layout_below="@+id/vehicleIconLive"
                        android:textSize="@dimen/textSizeNormal"
                        />
                </RelativeLayout>
                </android.support.v7.widget.CardView>
</RelativeLayout>
person Sreehari    schedule 10.02.2016

Чтобы сделать его совместимым как выше, так и ниже API 21, вам нужно указать app:cardUseCompatPadding="true" в вашей поддержке CardView.

кредит ShinChven повышение CardView не работает на Android 5.1.1

person Slava    schedule 26.02.2016