根据我的应用程序 ui 要求,它需要在 cardview 顶部有普通的一面,因此,它需要从上面移除阴影并触摸到另一个 View 。
我试过了
card_view:cardElevation="0dp"
但是它消除了四周的阴影,所以对我来说没用。
我尝试将负边距 (-5dp) 逐一应用于 CardView 和 ImageView ,但 CardView 始终覆盖图像而不是图像下方,所以它对我不起作用。
App ui需求是这样的
任何人都可以提出帮助解决这个问题的建议吗?
最佳答案
我通过创建看起来像卡片 View 但没有顶部阴影的自定义背景来解决这个问题。
在 drawable 文件夹中创建一个 XML 文件并将这段代码放入其中并在后台设置它。
android:background="@drawable/custom_cardview_no_top_shadow"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<padding android:top="0dp" android:right="10dp" android:bottom="5dp" android:left="10dp" />
<solid android:color="@color/transparent" />
</shape>
</item>
<item>
<shape>
<padding android:top="0dp" android:right="0dp" android:bottom="1dp" android:left="0dp" />
<solid android:color="@color/card_shadow_1" />
<corners android:radius="0dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="0dp" android:right="0dp" android:bottom="1dp" android:left="0dp" />
<solid android:color="@color/card_shadow_2" />
<corners android:radius="0dp" />
</shape>
</item>
<!-- Background -->
<item>
<shape >
<solid android:color="@color/card_background" />
<corners android:radius="0dp" />
</shape>
</item>
</layer-list>
这是卡片 View 的颜色
<!-- card colors -->
<color name="card_background">#ffffff</color>
<color name="card_shadow_1">#d4d4d4</color>
<color name="card_shadow_2">#dddddd</color>
<color name="transparent">#00000000</color>
关于android - 从一侧移除 CardView 阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41670321/