【问题标题】:Cardview shadow not appearing in lollipop devices?Cardview 阴影没有出现在棒棒糖设备中?
【发布时间】:2015-02-23 13:38:11
【问题描述】:

我在我的 android 应用程序中使用 cardview。然而阴影并没有出现。这是xml布局

默认的选项菜单阴影也没有显示。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ECEDF0"
    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"
        android:layout_gravity="center"
        android:clipChildren="false"
        card_view:cardBackgroundColor="@color/white"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="14dp"
        card_view:cardUseCompatPadding="true" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:text="Google Play" />
    </android.support.v7.widget.CardView>

</LinearLayout>

【问题讨论】:

  • 我认为linerlayout的背景和阴影的颜色比较相似。尝试更改衬里布局的颜色,然后检查它是否可见。
  • 改变了颜色但没有运气@PankajKumar
  • 牵强,但我已经看到这种情况发生,尝试删除 CardView 下方的 xmlns:card_view="schemas.android.com/apk/res-auto" 以便您只有在 LinearLayout 中的那个。
  • 试过但阴影不可见

标签: android android-layout android-5.0-lollipop android-cardview


【解决方案1】:

又翻了一遍文档,终于找到了解决办法。

只需将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="2sp"
    card_view:cardUseCompatPadding="true"
    >
...
</android.support.v7.widget.CardView>

【讨论】:

【解决方案2】:

正如那里提到的CardView not showing Shadow in Android L 确保您使用 hardwareAccelerated = true

绘制视图

hardwareAccelerated = true

hardwareAccelerated = false

详情请见Android Hardware Acceleration

【讨论】:

    【解决方案3】:

    对于 Lollipop 和更高版本,您应该在卡片上添加一些边距:

    <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="4dp"
                />
    

    由于阴影位于实际视图之外

    【讨论】:

    • 你拯救了我的一天。一些cardElevationcardUseCompatPadding 在没有Lollipop 利润的情况下无法正常工作。
    【解决方案4】:

    为某人,

    您应该注意的另一件事是,如果清单中有此行,阴影将不会显示:

    android:hardwareAccelerated="false"

    我尝试了所有建议的东西,但它只在我删除该行时才对我有用,我有该行的原因是因为我的应用程序与许多位图图像一起使用并且它们导致应用程序崩溃。

    【讨论】:

    • 谢谢@adnan,我做了很多搜索,无法在cardview上制作高程或阴影,但只需从 manifest.xml 中删除 android:hardwareAccelerated="false" 现在它是看起来像我需要的那样。
    【解决方案5】:

    请尝试输入 android:hardwareAccelerated="false" androidManifest 文件可能会解决您的问题,因为我也遇到了同样的问题并通过仅在清单中添加 1 行来解决。

    【讨论】:

      【解决方案6】:

      嘿朋友们,我得到了上述问题的解决方案。只需将此代码添加到您的 xml 中。

      <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_margin="8dp"
          android:id="@+id/griditem"
          android:layout_height="match_parent"
          card_view:cardUseCompatPadding="true"
          card_view:cardElevation="4dp"
          card_view:cardCornerRadius="3dp">`
      

      希望对你有帮助...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-12
        • 2016-02-04
        • 2016-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-29
        • 2015-09-15
        相关资源
        最近更新 更多