使用可绘制对象设置视图背景时,如何消除视图阴影。

5

我想将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>

问题在于当我选择一个项目时,可绘制对象会被很好地应用,但阴影完全消失而且没有放大!如果我不使用可绘制对象设置背景,则一切正常。阴影会按预期放大。 问题出在哪里?

看起来像是 这个 的重复。 - azizbekian
1个回答

4

这是一个从2014年开始的问题 https://issuetracker.google.com/issues/37008403


决策 1

不要使用透明度颜色作为 CardView 的背景


决策 2

为 CardView 内的 View 或 ViewGroup 设置背景,而不是设置 CardView 的背景,则一切正常工作。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接