[英]how to reduce fix shadow effect in recycler-view below item in android
这是我的图像,我使用卡片视图在 recyclerview 视图中显示项目。 下面是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_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/cardTicketName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="Here will be the text"
android:textColor="#484848"
android:padding="20dp"
android:textSize="15dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
项目底部阴影比顶部阴影更多,并且当我们将顶部项目滚动到底部然后底部的阴影增加以及再次滚动到顶部相同项目时,它的阴影效果法令也会发生同样的问题
如何解决这个问题?
我最近遇到了同样的问题,这是Ben P.所说的默认行为。 此外,还有更详细的解释。
通常,不建议与系统行为作斗争。 但是,如果您需要这样做 - 有一种简单的方法可以做到:
您可以使用MaterialShapeDrawable从官方的材料组件库(这是非常强大)为您的视图的背景。 它具有shadowCompatibilityMode
属性 - 您可以将其设置为MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS
,它将绘制假阴影而不是本机阴影。 它可能看起来像这样:
MaterialShapeDrawable().apply {
shadowCompatibilityMode = MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS // fake shadow instead of native one
setShadowColor(shadowColor) // you also can define shadow color
elevation = 4F
paintStyle = Paint.Style.FILL
}
因此,无论您在屏幕上的哪个位置,您都将拥有固定的阴影
Android 框架使用两个模拟光源的组合来创建阴影效果。 其中之一是一般的环境光,因此所有具有高度的物体都会在四面八方投下一点阴影。 然而,另一个是靠近屏幕顶部的模拟点光源。
这个点光源的位置意味着底部阴影总是大于顶部阴影。 这也意味着靠近屏幕底部的底部阴影总是大于靠近屏幕顶部的底部阴影。
无法禁用此行为。 如果你想使用 Android 的内置高程/阴影框架,这就是它的工作原理。
您可以通过自定义绘图或使用带有半透明黑色的渐变来模拟您自己的阴影,但是您会发现这两种方法都比简单地接受阴影的工作原理和用户所期望的要困难得多。
尝试这个
减少阴影减小卡的仰角大小。
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardElevation="1dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/cardTicketName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="Here will be the text"
android:textColor="#484848"
android:padding="20dp"
android:textSize="15dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
将card_view:cardElevation="0dp"
到 CardView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.