选择器selector中drawable和color的区别

夜深了!想想还是写篇博客再睡觉!
最近在项目中使用PagerSlidingTabStrip时,想变化选中字体颜色的变化,就想到了使用选择器。
注意!注意!注意!此为错误代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/android" >
    <item android:**drawable**="@color/white" android:state_selected="true"/>
    <item android:**drawable**="@color/gray" android:state_selected="false"/>
    </selector>

其中问题:就出现在drawable上,drawable应当引用的是一个drawable资源,而此时的应用的却是color值,故而取得不了效果!
所以正确的写法为:
drawable下的写法 ,而引用背景资源应该是background

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

color下的写法:
而字体选中时,引用时应该为:textcolor,而不是background
android:textcolor:@drawable/资源名


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

最后普及一下:后面选中的状态

android:state_press是否按下,如一个按钮触摸或者反击
androidstate_focused是否取得焦点,比如用户选择了一个文本框
android:state_hovered光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_selected被选中,它与focus state并不完全一样,如一个listView被选中的时候,他里面的各个子控件,可能通过方向键,被选中了。
android:state_checkable 组件是否能够被check.如RadioButton是可以被checkdandroid:state_checkedchecked了,如:一个RadioButton可以被cHeckandroid:state_enable 能够接受触摸或者点击事件
android:state_activated 被激活
android:state_window_focused 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了,有的需要加android:focusableandroid:clickabletrue才能获取焦点

最后:再次说明
android:drawable 放一个drawable资源
android:color放一个color资源
以上!

### 使用 Android Selector 实现不同状态下的 UI 效果 #### 创建 XML 文件定义选择器 为了实现在不同状态下显示不同的UI效果,在`res/drawable`目录下创建一个新的XML文件来定义选择器。此文件指定了各种可能的状态及其对应的绘制资源。 ```xml <!-- res/drawable/button_selector.xml --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 当按钮被禁用时应用的颜色或图片 --> <item android:drawable="@color/disabled_color" android:state_enabled="false"/> <!-- 当按下按钮时使用的颜色或图片 --> <item android:drawable="@color/pressed_color" android:state_pressed="true"/> <!-- 默认情况下(即没有任何特定状态激活时)使用的颜色或图片 --> <item android:drawable="@color/default_color"/> </selector> ``` 上述代码展示了如何设置当控件处于不同交互状态时应呈现的不同外观[^1]。 #### 将选择器应用于视图组件 一旦定义好了选择器,就可以将其作为背景或其他属性分配给任何支持这些特性的View对象。例如: ```java Button myButton = findViewById(R.id.my_button); myButton.setBackgroundResource(R.drawable.button_selector); // 设置自定义的选择器为背景 ``` 或者直接在布局文件中指定: ```xml <Button ... android:background="@drawable/button_selector" /> ``` 这使得每当用户的操作改变了该视图的状态——比如点击它或是启用/禁用了这个元素——都会触发相应的变化并展示预设的效果[^2]。 #### 自定义属性扩展功能 除了基本的状态切换外,还可以利用自定义属性进一步增强选择器的功能。例如,可以通过调用`setEnabled()`方法改变一个View的有效性状态从而影响其Drawable的表现形式。 另外值得注意的是,除了简单的颜色变化之外,也可以引入更复杂的图形变换如渐变、波纹等特效来丰富用户体验[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值