Android L 设置background为?android:attr/selectableItemBackgroundBorderless问题小结

一次开发中,在给一个Textiew设置background为?android:attr/selectableItemBackgroundBorderless时,发现没效果,简化后的布局如下代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff" >
    </FrameLayout>


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:background="?android:attr/selectableItemBackgroundBorderless"
            android:clickable="true"
            android:gravity="center_horizontal"
            android:text="测试点击效果" />
    </FrameLayout>


</FrameLayout>

经过一番研究,把上面的代码改成了下面的布局,去掉了一个背景色,结果可以了,如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        >
    </FrameLayout>


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:background="?android:attr/selectableItemBackgroundBorderless"
            android:clickable="true"
            android:gravity="center_horizontal"
            android:text="测试点击效果" />
    </FrameLayout>


</FrameLayout>

进一步探索,在原基础上给TextView的父亲设上一个背景,结果也可以,代码如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff" >
    </FrameLayout>


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff" >


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:background="?android:attr/selectableItemBackgroundBorderless"
            android:clickable="true"
            android:gravity="center_horizontal"
            android:text="测试点击效果" />
    </FrameLayout>


</FrameLayout>

......

经过几番探索后,得出一个结论,设置selectableItemBackgroundBorderless后,会从孩子往父亲找一个依附的色。对于我遇到的问题,就是因为TextView往上找的时候,亲生父亲没背景色,然后继续找到了最顶端的爷爷,这个时候才绘制。然而,父亲的兄弟又绘制了颜色,且盖住了最顶端的绘制,导致看不到效果。如果把#ffffff改成#ddffffff,有一定的透明度,结果就显而易见了。

特别注意:当把硬件加速给关闭时,这个效果是没有的。


  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值