CardView не показывает Shadow в Android L
My Cardview внутри Listview не показывает тень в Android L (Nexus 5). Также круглые края не отображаются должным образом. Вот код для просмотра списка просмотра:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res/com.example.myapp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@android:color/white"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="4dp"
app:cardElevation="4dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="@dimen/padding_small"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="@+id/ivPicture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvName"
android:layout_centerHorizontal="true"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/tvDetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ivPicture"
android:layout_centerHorizontal="true"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" />
</RelativeLayout>
</android.support.v7.widget.CardView>
И ListView xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/com.example.myapp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/app_bg" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:cacheColorHint="#00000000"
android:divider="@android:color/transparent"
android:drawSelectorOnTop="true"
android:smoothScrollbar="true" />
<ProgressBar
android:id="@+id/progressBarMain"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" /></RelativeLayout>
Он отлично работает на устройствах pre-L с правильными тенями и закругленными углами. Но не работает устройство Android L. Можете ли вы рассказать, что мне не хватает здесь?
Ответы
Ответ 1
Наконец, я смог получить тени на устройстве Lollipop, добавив маржу к карте. Вот окончательный макет карты:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res/com.example.myapp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@android:color/white"
android:foreground="?android:attr/selectableItemBackground"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/padding_small"
android:layout_marginBottom="@dimen/padding_small"
app:cardCornerRadius="4dp"
app:cardElevation="4dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="@dimen/padding_small"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="@+id/ivPicture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvName"
android:layout_centerHorizontal="true"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/tvDetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ivPicture"
android:layout_centerHorizontal="true"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" />
</RelativeLayout>
Ответ 2
После просмотра документов снова я нашел решение.
Просто добавьте card_view:cardUseCompatPadding="true"
к вашему CardView
, и тени появятся на устройствах Lollipop.
Что происходит, область содержимого в CardView
принимает разные размеры на устройствах с предварительным леоптипом и леденцом. Таким образом, в устройствах с леденец тень на самом деле покрыта картой, поэтому ее не видно. Добавляя этот атрибут, область содержимого остается неизменной для всех устройств, и тень становится видимой.
Мой xml-код похож:
<android.support.v7.widget.CardView
android:id="@+id/media_card_view"
android:layout_width="match_parent"
android:layout_height="130dp"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardElevation="2dp"
card_view:cardUseCompatPadding="true"
>
...
</android.support.v7.widget.CardView>
Ответ 3
используйте app:cardUseCompatPadding="true"
внутри вашей карты.
Для примера
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/cardviewMarginRight"
app:cardBackgroundColor="@color/menudetailsbgcolor"
app:cardCornerRadius="@dimen/cardCornerRadius"
app:cardUseCompatPadding="true"
app:elevation="0dp">
</android.support.v7.widget.CardView>
Ответ 4
Не забывайте, что для рисования тени вы должны использовать hardwareAccelerated
рисунок
hardwareAccelerated = true
![введите описание изображения здесь]()
hardwareAccelerated = false
![hardwareAccelerated CardView]()
Подробнее см. Android Hardware Acceleration
Ответ 5
check hardwareAccelerated в манифесте делает его истинным, делая его ложным, удаляет тени, когда ложная тень появляется в предварительном просмотре xml, но не в телефоне.
Ответ 6
Добавьте эту строку в CardView....
card_view:cardUseCompatPadding="true" //for enable shadow
card_view:cardElevation="9dp" // this for how much shadow you want to show
подсказки
Вы можете избежать layout_marginTop и layout_marginBottom, так как сама тень занимает некоторое пространство вверх и вниз. Размер пространства определяется тем, сколько вы будете использовать в card_view: cardElevation = "ndp".
Счастливое кодирование (:
Ответ 7
Вы можете добавить эту строку кода для тени в виде карты
card_view:cardElevation="3dp"
Ниже приведен пример
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@android:color/white"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardElevation="3dp"
card_view:cardCornerRadius="4dp">
Надеюсь, это поможет!
Ответ 8
Просто добавьте эти теги:
app:cardElevation="2dp"
app:cardUseCompatPadding="true"
Итак, он станет:
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="?colorPrimary"
app:cardCornerRadius="3dp"
app:cardElevation="2dp"
app:cardUseCompatPadding="true"
app:contentPadding="1dp" />
Ответ 9
Вы можете попробовать, добавив эту строку
card_view:cardUseCompatPadding="true"
Весь код будет выглядеть следующим образом
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="5dp"
android:orientation="horizontal"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="5dp">
</android.support.v7.widget.CardView
Ответ 10
В моем случае тень не показывалась на устройствах Android L, потому что я не добавлял маржи. Проблема в том, что CardView автоматически создает этот маркер на < 5 устройствах, поэтому теперь я делаю это следующим образом:
CardView card = new CardView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
if (Build.VERSION_CODES.LOLLIPOP == Build.VERSION.SDK_INT) {
params.setMargins(shadowSize, shadowSize, shadowSize,
shadowSize);
} else {
card.setMaxCardElevation(shadowSize);
}
card.setCardElevation(shadowSize);
card.setLayoutParams(params);
Ответ 11
Прежде всего убедитесь, что следующие зависимости правильно добавлены и скомпилированы в файле build.gradle
dependencies {
...
compile 'com.android.support:cardview-v7:21.0.+'
}
и после этого попробуйте следующий код:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="5dp"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp">
</android.support.v7.widget.CardView
проблема возникает в Android L beacuse Атрибут layout_margin не добавлен
Ответ 12
Добавьте android: hardwareAccelerated = "true" в файл манифеста, как показано ниже, у меня работает
<activity
android:name=".activity.MyCardViewActivity"
android:hardwareAccelerated="true"></activity>
Ответ 13
переместите свой атрибут "uses-sdk" в верхней части манифеста, как показано ниже: https://stackoverflow.com/a/27658827/1394139
Ответ 14
Мой recyclerview был медленным в загрузке, поэтому, прочитав stackoverflow.com, я изменил hardwareAccelerated на "false", тогда высота не отображается в устройстве. Я вернулась к истине. Меня устраивает.
Ответ 15
Вид карты не может отображать тень, поскольку RelativeLayout
находится на тени вида карты. Чтобы показать тень, добавьте поля на вашем Карточном представлении. Например:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="8dp">
</android.support.v7.widget.CardView>
</RelativeLayout>
Ответ 16
Я думаю, если вы проверяете ваш манифест первый, если вы написали android:hardwareAccelerated="false"
, вы должны сделать это true
к тому, тени для карты Как этот ответ здесь
Ответ 17
android:hardwareAccelerated="true"
сделал трюк!