我偶然发现了一些奇怪的事情。我在我的应用程序中的多个位置使用相同的CardView
组件,所有组件都具有相同的高度(3dp),并且它们看起来都相同。但是,对于一种情况,我需要将CardView
嵌入到ScrollView
中。我没有更改CardView
配置中的任何内容,但阴影现在看起来不同了。
这是我的布局:
<ScrollView 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="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:padding="16dp"
android:scrollbars="none">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
app:cardCornerRadius="8dp"
app:cardElevation="3dp"
app:cardUseCompatPadding="true">
<!-- some other views-->
</androidx.cardview.widget.CardView>
</ScrollView>
在下面的图片中,您可以看到右侧的CardView
,左侧是两个阴影:顶部是单独的CardView
生成的阴影,底部是CardView
位于ScrollView
内部时产生的阴影。如你所见,第二个看起来更暗更厚。
注意:两种方案中的背景颜色(#FAFAFA)都相同。
对于这两种情况,我会检查以下属性,因为它可能会导致您看到的差异:
getTranslationZ()
getElevation()
getOutlineAmbientShadowColor()
(仅在API 28+中有效)getOutlineSpotShadowColor()
(仅在API 28+中有效)另外,检查布局检查器以查看是否存在可能影响渲染的任何视图。
此外,Sebastiano Poggi的this article可能会有所帮助。