android - 查看 setBackground 与 drawable 删除海拔的阴影

标签 android kotlin material-design android-drawable android-elevation

我尝试在我的 RecyclerView 中实现 Material Design 的高程概念。

对于 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>

当我选择项目时,我以编程方式将可绘制对象应用到背景以获得效果,并按照 Material Design 规范的建议将高度设置为 8dp。
        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 编码)
可绘制对象:
<?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>

问题是当我选择一个项目时,可绘制对象被很好地应用但阴影被完全移除并且没有被放大!
如果我没有用drawable设置背景,它的效果很好。阴影像预期的那样被放大。

问题是什么 ?

最佳答案

从2014年起是麻烦https://issuetracker.google.com/issues/37008403

决定 1

不要对背景 CardView 使用具有透明度的颜色

决定 2

为 CardView 内的 View 或 ViewGroup 设置背景而不是这个 cardView,然后一切正常

enter image description here

关于android - 查看 setBackground 与 drawable 删除海拔的阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48528576/

相关文章:

Android/Kotlin : Why is the lambda syntax different for . setOnClickListener() 和 .observe()?

javascript - 在 reactjs Material ui 进度条中显示百分比数

jquery - 使用 Material 设计精简版创建星级

android - 如何在 Android 的 ContentResolver.query() 中防止 gmail 联系人?

API 26 上未显示 Android 通知

android - Nexus 7 接近传感器

java - Kotlin - Jackson 忽略空值

android - 更改搜索 View 中的默认搜索图标位置

java - 无需用户交互即可开始视频聊天 - Android

android - 上传到 Play 商店后 GCM 停止工作