Android实战—selector使用之改变控件背景

    xiaoxiao2021-04-13  37

    Android中的Selector主要是用来改变ListView和Button控件的默认背景。其使用方法可以按一下步骤来设计:

    (以在list_view.xml为例)

    1.创建list_view.xml文件

    首先在res目录下新建drawable文件夹,再在新建的drawable文件夹中新建list_view.xml,其目录结构为:res/drawable/list_view.xml。

    2.根据具体需求编辑list_view.xml文件

    新建list_view.xml文件后,在没有添加任何属性时其内部代码结构为:

    <?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> </selector> 下面就可以根据项目需求,在其内部定义为自己想要的样式了,主要属性如下:

    <?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 默认时的背景图片--> <item android:drawable="@drawable/pic1" /> <!-- 没有焦点时的背景图片 --> <item android:state_window_focused="false" android:drawable="@drawable/pic1" /> <!-- 非触摸模式下获得焦点并单击时的背景图片 --> <item android:state_focused="true" android:state_pressed="true" android:drawable= "@drawable/pic2" /> <!-- 触摸模式下单击时的背景图片--> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/pic3" /> <!--选中时的图片背景--> <item android:state_selected="true" android:drawable="@drawable/pic4" /> <!--获得焦点时的图片背景--> <item android:state_focused="true" android:drawable="@drawable/pic5" /> </selector>3.引用 list_view.xml文件

    三种方法可以来引用刚才创建的文件:

    (1)在ListView中添加如下属性代码

    android:listSelector="@drawable/mylist_view" (2)在 ListView的item界面中添加如下属性代码 android:background="@drawable/mylist_view"(3)利用JAVA代码直接编写

    Drawable drawable = getResources().getDrawable(R.drawable.mylist_view); listView.setSelector(drawable);为了防止列表拉黑的情况发生,需要在 ListView中添加以下的属性代码

    android:cacheColorHint="@android:color/transparent"属性介绍:

    android:state_selected选中

    android:state_focused获得焦点

    android:state_pressed点击

    android:state_enabled设置是否响应事件,指所有事件

    Selector drawable的item中可以有以下属性: 

    Android:drawable="@[package:]drawable/drawable_resource" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_selected=["true" | "false"] android:state_active=["true" | "false"] android:state_checkable=["true" | "false"]  android:state_checked=["true" | "false"]  android:state_enabled=["true" | "false"]  android:state_window_focused=["true" | "false"]

    <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="true" android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/enabled_on_pressed" /> <item android:state_enabled="true" android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/enabled_off_pressed" /> <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/enabled_on" /> <item android:state_enabled="true" android:state_checked="false" android:drawable="@drawable/enabled_off" /> <item android:state_enabled="false" android:state_checked="true" android:drawable="@drawable/disabled_on" /> <item android:state_enabled="false" android:state_checked="false" android:drawable="@drawable/disabled_off" /> </selector> Item顺序是有讲究的,条件限定越细致,则应该放到前面。比如这儿如果把1,2行和3,4行的item交换,那么pressed的就永远无法触发了,因为有item已经满足条件返回了。可以理解为代码中的if语句。

    转载请注明原文地址: https://ju.6miu.com/read-668535.html

    最新回复(0)