Android LinearLayout使用selector改变交互时背景颜色
  xaeiTka4h8LY 2024年08月09日 32 0
换言之,就像Android Button一样,基于布局文件,把LinearLayout做成一个可以在用户交互触摸点击时候背景颜色有所改变的控件。
具体方法:
(1)在LinearLayout属性中写:
 
android:background="@drawable/selector"

(2)在drawable目录下新建selector.xml文件,设置状态和焦点事件响应的背景。
(3)如果selector.xml文引用了自定义颜色,在values目录下新建color.xml,写颜色的属性值。

例如:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http:///apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/selector"
    android:clickable="true"
    android:orientation="vertical" >
</LinearLayout>

activity_main.xml中设置了background,在drawable下新建selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http:///apk/res/android">
    <item android:state_focused="true" android:drawable="@color/red_transparent"/>
    <item android:state_pressed="true" android:drawable="@color/red_transparent" />
    
    <item android:drawable="@color/red"/>
</selector>

selector.xml中又引用了自定义颜色,那么就需要在values目录下建立color.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#ff5654</color>
    <color name="red_transparent">#ddff5654</color>
</resources>
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年08月09日 0

暂无评论

xaeiTka4h8LY