【问题标题】:CardView elevation not working on Android 5.1.1CardView 海拔在 Android 5.1.1 上不起作用
【发布时间】:2015-08-02 15:12:26
【问题描述】:

我在 RecyclerView 中使用 CardView。 在阅读了很多之后,我最终只在 Android 5.1.1 上遵循了“不工作”代码。在此之前的 Android 版本上,它运行良好。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
    android:background="#11ffffff"
    android:orientation="vertical"
    android:paddingBottom="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#11ffffff">
        <android.support.v7.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@color/primary_bg_light"
            card_view:cardCornerRadius="2dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/primary_bg_light">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:paddingBottom="15dp"
                        android:paddingLeft="15dp"
                        android:textColor="@color/background_floating_material_light"
                        android:textSize="24sp"
                        android:textStyle="bold" />

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

我也尝试使用代码设置海拔,但结果相同。

【问题讨论】:

  • 抱歉,我没有看到您在此代码中设置海拔高度的位置..
  • 属性card_view:cardElevation在哪里?
  • 另外,CardView 的父级 RelativeLayout 看起来是多余的。我认为背景 #11ffffff 几乎是黑色的,并且在设置高度时可能看不到卡片阴影
  • @Blackbelt:我确实尝试过使用 xml 和代码的提升,当在 RecyclerView 中使用 Card View 时,两者都不适用于 5.1.1。
  • @GennadiiSaprykin :是的可能是多余的,但我这样做是因为它是针对 Stack Overflow 上类似问题的建议解决方案。即使没有背景颜色,它也不起作用。

标签: android android-cardview android-5.1.1-lollipop


【解决方案1】:

要使其在 api 21 以上或以下兼容,您需要在支持 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>

【讨论】:

    【解决方案2】:

    只需将此代码添加到您的 xml 文件中的卡片视图!

        card_view:cardUseCompatPadding="true"
    

    您必须使用 Alt+Enter 导入 card_view ! 下面的代码将在顶部导入!。

      xmlns:card_view="http://schemas.android.com/apk/res-auto"
    

    您会看到海拔在 android 5 或 5+ 上运行。

    【讨论】:

      【解决方案3】:

      如果您在AndroidManifest.xml中使用过,请删除以下行

      android:hardwareAccelerated="false"
      

      【讨论】:

        【解决方案4】:

        从 AndroidManifest.xml 文件中的 &lt;application ...&gt; &lt;/application&gt; 中删除这一行

        android:hardwareAccelerated="false"
        

        改成

        android:hardwareAccelerated="true"
        

        更多详情Hardware acceleration

        【讨论】:

          【解决方案5】:

          在卡片视图中设置

          <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="match_parent"
          android:layout_margin="6dp"
          card_view:cardUseCompatPadding="true"
          card_view:cardElevation="4dp"
          card_view:cardCornerRadius="3dp">
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-03-05
            • 2021-05-05
            • 2015-01-11
            • 2018-01-13
            • 2016-05-22
            • 2019-03-23
            • 2017-06-11
            • 2019-01-03
            相关资源
            最近更新 更多