美文网首页
selector (背景选择器)

selector (背景选择器)

作者: 穿越平行宇宙 | 来源:发表于2019-04-02 10:02 被阅读0次

Android中 selector 主要用于对控件(如对 button ,imagebutton,textview 等)的背景选择。

  • 属性介绍:

android:state_selected: 是否选中 true or false

android:state_focused: 是否获得焦点 true or false

android:state_pressed : 是否点击 true or false

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

android:state_checkable:组件是否能被check(选中),表示是否选中的状态。如:RadioButton(单选按钮)是可以被check的。

android:state_checked: 表示能不能选中,如:一个RadioButton可以被check了。

android:state_enabled: 能够接受触摸或者点击事件

注意:如果有多个item,那么程序将自动从上到下进行匹配,最先匹配的将得到应用。(不是通过最佳匹配)
如果一个item没有任何的状态说明,那么它将可以被任何一个状态匹配。


  • 用法介绍:

第一步,在目录 res新建一个drawable文件夹,或者直接写在drawable-hdpi 等,新建一个xml文件。

Android中 <wbr>selector(背景选择器)的作用和用法简介。

第二步,写好后直接生成模板,只要自己添加,相关属性。

Android中 <wbr>selector(背景选择器)的作用和用法简介。
第三步,在写好的main.xml文件中,定义的控件中去。 Android中 <wbr>selector(背景选择器)的作用和用法简介。
  • 给ListView设置Selector
  1. 创建mylist_view.xml文件

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

  1. 根据具体需求编辑mylist_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>
  1. 引用mylist_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"

  • Selector是用来改变控件在不同状态(如:点击和不点击)下的行为,可以是颜色,或者上面的字。<shape>和<selector>在Android UI设计中经常用到。比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到<shape>和<selector>。可以这样说,<shape>和<selector>在美化控件中的作用是至关重要。先来个例子
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" ><!-- 选中状态 -->
        <layer-list ><!-- 将多个图片或两种效果按照顺序层叠起来 -->
            <item >
                <shape android:shape="rectangle"><!-- 可选rectagle矩形,oval椭圆,line水平直线,ring环形 -->
                    <stroke android:width="5dp"  android:color="#ff0000"/>
                </shape>
            </item>
            <item  android:bottom="5dp" >
                <shape android:shape="rectangle" >
                    <solid android:color="#fff"/><!--描边-->
                </shape>
            </item>
        </layer-list>
    </item>
    <item ><!-- 默认状态 -->
        <shape >
            <solid  android:color="@color/light_blue"/>
        </shape>
    </item>
</selector>

相关文章

网友评论

      本文标题:selector (背景选择器)

      本文链接:https://www.haomeiwen.com/subject/fkhwbqtx.html