我想将Material Design的高度概念应用到我的RecyclerView中。
对于RecyclerView的每个项目,我使用以下内容:
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="0dp"
android:elevation="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
.../>
<CheckBox
.../>
</RelativeLayout>
</android.support.v7.widget.CardView>
当我选择一个项目时,我会通过程序将一个可绘制对象应用于背景以产生效果,并将高程设置为8dp,根据Material Design规范的建议。
val draw = ResourcesCompat.getDrawable(resources, R.drawable.selected_task, null)
draw?.setColorFilter(task.color.aRGB.toInt(), PorterDuff.Mode.OVERLAY)
view.background = draw
view.elevation = 8F
(我使用Kotlin编程) 这个drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="2dp"
android:color="@color/select_border"/>
<solid
android:color="@color/select_fill"/>
</shape>
颜色值
<color name="select_border">#78000000</color>
<color name="select_fill">#32000000</color>
问题在于当我选择一个项目时,可绘制对象会被很好地应用,但阴影完全消失而且没有放大!如果我不使用可绘制对象设置背景,则一切正常。阴影会按预期放大。 问题出在哪里?