我的Cardview在Android L(Nexus 5)中没有显示阴影.此外,圆边未正确显示.以下是Listview的适配器视图的代码:

<?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设备上运行.你能说出我在这里错过了什么吗?

推荐答案

在再次查看文档后,我终于找到了解决方案.

只要在CardView的基础上加card_view:cardUseCompatPadding="true",棒棒糖设备上就会出现阴影.

结果是,CardView的内容区在pre-lollipop和lollipop设备上的大小不同.所以在棒棒糖设备中,阴影实际上被卡片覆盖,所以它不可见.通过添加此属性,所有设备上的内容区域保持不变,阴影变得可见.

我的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>

Android相关问答推荐

如何清除导航抽屉中以前 Select 的项目(Jetpack Compose)

无法在Jetpack Compose中显示Admob原生广告

Jetpack Compose-如何使用值动画直接控制其他动画

尽管我们不再使用GCM SDK,但应用程序已被标记为使用GCM SDK

如何在 Jetpack Compose LazyColumn 中将项目分组在一起,例如卡片

使用 List 和 LazyColumn 重新组合所有项目

在 kotlin 协同 routine 中,如何将数据范围限定为请求路径(以 MDC 为例)?

在 MVVM Jetpack Compose 上添加依赖项时出现重复类错误

用作输入参数的 Lambda 函数导致重组

如何在 Jetpack Compose 中向图像视图添加对角色带?

如何使用 Jetpack Compose 在图像上叠加文本

可组合的可见性不随状态变化而变化

运行一次 kotlin 流,但在下游收到两次

在开发过程中我应该把 mp4 文件放在哪里?

在 Jetpack Compose 中 Select 要省略的文本

Android全屏AlertDialog

如何从构建的流对象中发出值

使用 Android 字符串数组在 Room 中迁移

如何将私有 mutableStateOf 分配给 Android Jetpack 中的 State 变量?

实际类型别名ApplicationContext没有相应的预期声明