【问题标题】:CardView elevation not working with RecyclerViewCardView 海拔不适用于 RecyclerView
【发布时间】:2018-01-13 04:27:02
【问题描述】:

在单独的应用程序中进行测试时,CardView 高程可以正常工作,但是当 cardView 的相同代码用于制作 RecyclerView 的项目时,高程不再出现。

这里是 RecyclerView 列表项的代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_height="wrap_content"
             android:layout_width="match_parent"
             xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:card_view="http://schemas.android.com/apk/res-auto"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             android:layout_margin="10dp">


<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorAccent"
    card_view:cardUseCompatPadding="true"
    app:cardMaxElevation="6dp"
    card_view:cardCornerRadius="3dp"
    card_view:cardElevation="24dp">

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

        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="match_parent"
            android:layout_height="250dp" />

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/thumbnail"
            />
    </LinearLayout>


</android.support.v7.widget.CardView>

这是我的主要活动 xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                   xmlns:tools="http://schemas.android.com/tools"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:orientation="vertical"
                   android:background="@drawable/bitmap"
                   android:layerType="software"
                   android:padding="10dp"
    tools:context="com.example.android.vikramadityastimeline.MainActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/card_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    />



 </FrameLayout>

【问题讨论】:

  • 现在是什么样子的?可以分享截图吗?
  • 你应该为 FrameLayout 设置填充...
  • 试过..没用
  • 我正面临与此相关的问题。如果我在onBindViewHolder() 中更改cardView 的背景,它的阴影不会显示。
  • 将此添加到您的 xml 中的 CardView。应用程序:cardElevation="2dp"。将 2dp 更改为您想要的任何值。

标签: android android-recyclerview android-cardview android-elevation


【解决方案1】:

这是下面截图的xml示例代码:

<androidx.cardview.widget.CardView 
    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:cardCornerRadius="10dp"
    app:cardElevation="2dp"
    app:cardPreventCornerOverlap="true"
    app:cardUseCompatPadding="true"
    app:contentPadding="10dp"
    app:contentPaddingBottom="0dp">

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

        <TextView
            android:id="@+id/name_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Name" />
        <TextView
            android:id="@+id/sim_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SIM" />
        <TextView
            android:id="@+id/ID_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ID" />
        <TextView
            android:id="@+id/model_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="model" />
    </LinearLayout>

</androidx.cardview.widget.CardView>

【讨论】:

    【解决方案2】:

    我刚刚遇到了同样的问题,我找到的解决方案是在 AndroidManifest 中添加下一个标签:

    android:hardwareAccelerated="true"
    

    例子:

    <application
            android:allowBackup="true"
            android:hardwareAccelerated="true"
            android:icon="@mipmap/ic_launcher"
            android:theme="@style/AppTheme">
    

    文档:

    https://developer.android.com/guide/topics/graphics/hardware-accel

    【讨论】:

      【解决方案3】:

      在您的卡片视图中使用它。它解决了我同样的问题。

      app:cardUseCompatPadding="true"
      

      【讨论】:

        【解决方案4】:

        在您的代码中,您在回收站视图项中使用了框架布局,并且在其中您有卡片视图。 您为框架布局提供 10dp 的边距,但我对您的建议是删除该行并为卡片视图添加边距。 在回收站视图中,项目之间没有空间,因此您的卡片视图的阴影可能会被隐藏,因为您的外框布局在那里完成,因此请尝试像这样更改您的代码:

            <?xml version="1.0" encoding="utf-8"?>
        <FrameLayout android:layout_height="wrap_content"
                     android:layout_width="match_parent"
                     xmlns:android="http://schemas.android.com/apk/res/android"
                     xmlns:card_view="http://schemas.android.com/apk/res-auto"
                     xmlns:app="http://schemas.android.com/apk/res-auto"
                     android:padding="10dp">
        
        
        <android.support.v7.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            app:cardUseCompatPadding="true"
            app:cardMaxElevation="6dp"
            android:layout_marginBottom="10dp"
            app:cardCornerRadius="3dp"
            app:cardElevation="24dp">
        
            <LinearLayout android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
        
                <ImageView
                    android:id="@+id/thumbnail"
                    android:layout_width="match_parent"
                    android:layout_height="250dp" />
        
                <TextView
                    android:id="@+id/title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/thumbnail"
                    />
            </LinearLayout>
        
        
        </android.support.v7.widget.CardView> 
        </FrameLayout>
        

        【讨论】:

        • 试过了,仍然无法正常工作,并且海拔属性也不适用于任何其他元素,例如我的应用程序中的 textView 或浮动按钮。然而,对于我为测试这一点而创建的另一个示例应用程序,海拔工作正常。而且 xml 预览显示已经添加了海拔,但当我在设备上运行它时没有。
        • 好的.. 现在尝试将所有 card_view:... 替换为 app:... 并在您的代码中检查它,在运行时它没有显示高度,这将很有帮助。
        【解决方案5】:

        要支持它,您需要在您的支持 CardView 中指定app:cardUseCompatPadding="true"

        <android.support.v7.widget.CardView
                app:cardElevation="4dp"
                app:cardUseCompatPadding="true"
                app:cardMaxElevation="6dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
        </android.support.v7.widget.CardView>
        

        【讨论】:

        • app:cardUseCompatPadding="true",这条线解决了我的问题
        【解决方案6】:

        将此行添加到您的CardView xml 标记中;

        android:layout_margin="10dp"
        

        【讨论】:

        • 从您的 xml 中删除这 2 行并运行.... card_view:cardUseCompatPadding="true" app:cardMaxElevation="6dp"
        • 还是没有效果。
        • 从 RecyclerView 列表项中移除 FrameLayout
        猜你喜欢
        • 2015-08-18
        • 1970-01-01
        • 2019-03-23
        • 2017-06-11
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        • 2017-07-13
        • 2021-07-19
        相关资源
        最近更新 更多