【问题标题】:View setBackground with drawable remove the elevation's shadows查看 setBackground 与 drawable 删除海拔的阴影
【发布时间】:2018-07-09 18:15:45
【问题描述】:

我尝试在我的 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 被很好地应用了,但是阴影被完全移除并且没有被放大! 如果我没有用drawable设置背景,它的效果很好。阴影像预期的那样被放大了。

有什么问题?

【问题讨论】:

  • 好像是this的副本。

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


【解决方案1】:

从2014年开始麻烦https://issuetracker.google.com/issues/37008403


决定 1

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


决定 2

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-22
    • 2015-02-13
    • 2016-02-03
    • 2017-10-15
    • 2015-02-14
    • 1970-01-01
    • 2016-11-11
    • 2017-07-13
    相关资源
    最近更新 更多