安卓 Selector 标签 (背景选择器 OR 颜色选择器)

安卓 Selector 标签 (背景选择器 OR 颜色选择器)

背景选择器

  Selector是安卓xml资源文件里的一种常用标签,其文件通常存放于drawable下做为背景选择器,又有的人称之为状态切换器,即根据控件的状态显示对应设置的背景。一个简单的背景选择器如下。使用上的话,就如同设置普通背景图一样设置。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:drawable="@color/colorPrimary" android:state_selected="true" />
    <item  android:drawable="@color/colorPrimary" android:state_pressed="true" />
    <item  android:drawable="@color/colorAccent"  />
</selector>

既然是设置背景,再根据状态进行切换,那么如下:

  1. 每个<item></item>设置一个切换状态。
  2. item中设置对应的资源文件及状态,状态值为 true 或 false,例如 android:state_selected="true"即表示选中时生效,反之,则为非选中状态下生效。
  3. 同时存在多个状态时,优先级从上到下,所以无状态默认值写在最下面。

Ok,咱看看有哪些状态。

  • android:drawable
    设置资源文件,背景色,图片均可。

  • android:state_pressed
    按压状态,默认为false。

  • android:state_focused
    获得焦点,默认false。

  • android:state_hovered
    光标移动到控件上,默认false。

  • android:state_selected
    控件被选择,默认false。

  • android:state_checkable,类似下拉列表,可代码设置为选择状态。
    当控件可以被勾选时,这个仅在当控件具有被勾选和不被勾选的状态间转换时才起作用。

  • android:state_checked
    控件被勾选,类似于CheckBox被勾选那种。

  • android:state_enabled
    控件处于可用的状态,比如可以被点击。

  • android:state_activated
    控件处于激活的状态

  • android:state_window_focused
    应用窗口获得焦点的状态,控件处于最前端时。

  Selector在使用上也是非常简便的:

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_01"
        android:gravity="center"
        android:clickable="true"
        android:text="selector" />

颜色选择器

  以上,咱说的是背景选择器,使用的是 drwable 属性,通常是让背景根据控件的状态改变,如果我们要将其作为一个颜色选择器,让控件的字体颜色随控件的状态改变怎么办?也很简单,首先使用的属性对应更改为 color ,存放的位置由原来的 res - drwable 变成了 res - color ,简单使用如下。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item android:color="@color/colorPrimary" android:state_focused="true"/>
    <item  android:color="@color/colorPrimary" android:state_pressed="true" />
    <item  android:color="@color/colorAccent"/>
</selector>

在这里插入图片描述
  存放位置改变,每个 item 中设置的属性改变。使用如下:@color/资源文件

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:clickable="true"
        android:gravity="center"
        android:padding="8dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="textColor selector"
        android:textColor="@color/selector_02" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@null"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center"
        android:padding="8dp"
        android:text="EditText selector "
        android:textColor="@color/selector_02" />

  值得注意的是,通常我们需要 ,以下:

  • android:clickable=“true” 允许被点击
  • android:focusable=“true” 允许获得焦点
  • android:focusableInTouchMode=“true” 允许通过触摸得到焦点

  来配合选择器的使用,因为有些控件本身不能点击,也无法获得焦点,需要设置一下。

  Demo获取.

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值