使用selector改变文字的颜色
2015-07-30 14:25
435 查看
有时候为了提高用户体验,我们希望有些文字按下去的时候改变颜色,这时候就要用到selector了。代码如下。
首先在drawable文件夹下定义一个selector文件:如下(我的应用场景是 相对布局中有一行文字,点击相对布局文字颜色改变,state选的是pressed )
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- drawable -->
<item android:state_pressed="false" android:drawable="@color/bg_gray6"/>
<item android:state_pressed="true" android:drawable="@color/e1e1e1"/>
</selector>
刚开始也以为定义好这个文件后,在布局文件中的Textview中直接引用就可以,但是没有效果,后来才知道还需要再加两行其他的代码,如下:(很关键,加上后想要的就出现了,在父控件中添加android:clickable=“true” android:focusable=“true”,而在子控件中添加android:duplicateParentState=“true”子控件就能获得父控件的点击事件)
<RelativeLayout
android:id="@+id/rl_cleancache"
android:layout_width="fill_parent"
android:layout_height="38dp"
android:clickable="true"
android:focusable="true"
android:background="@drawable/my_bg_selector"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:duplicateParentState="true"
android:layout_marginLeft="10dp"
android:textColor="@color/setting_color_selector"
android:textSize="@dimen/font_size_normal"
android:text="清除缓存"
/>
</RelativeLayout>
首先在drawable文件夹下定义一个selector文件:如下(我的应用场景是 相对布局中有一行文字,点击相对布局文字颜色改变,state选的是pressed )
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- drawable -->
<item android:state_pressed="false" android:drawable="@color/bg_gray6"/>
<item android:state_pressed="true" android:drawable="@color/e1e1e1"/>
</selector>
刚开始也以为定义好这个文件后,在布局文件中的Textview中直接引用就可以,但是没有效果,后来才知道还需要再加两行其他的代码,如下:(很关键,加上后想要的就出现了,在父控件中添加android:clickable=“true” android:focusable=“true”,而在子控件中添加android:duplicateParentState=“true”子控件就能获得父控件的点击事件)
<RelativeLayout
android:id="@+id/rl_cleancache"
android:layout_width="fill_parent"
android:layout_height="38dp"
android:clickable="true"
android:focusable="true"
android:background="@drawable/my_bg_selector"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:duplicateParentState="true"
android:layout_marginLeft="10dp"
android:textColor="@color/setting_color_selector"
android:textSize="@dimen/font_size_normal"
android:text="清除缓存"
/>
</RelativeLayout>
相关文章推荐
- sqlserver完整备份一个新库的方法
- httpWebRequest 文件下载
- HDU 1950~Bridging signals~二分法求解
- javascript 解析json的代码
- spring mvc文件上传
- 庆祝自己第一次开通博客
- 所有游戏类型简称的意思
- PHP图片压缩
- iOS文字的高度
- 【Android应用开发技术:数据存储】SharedPreferences
- nvelocity模板引擎
- LeetCode --- 67. Add Binary
- MAC地址和IP地址的关系
- PAT (Advanced Level) 1083. List Grades (25) 结构体排序
- hdoj 2199 Can you solve this equation?
- 利用HttpWebRequest访问WebApi
- 在cisco交换机上禁止某个特定MAC地址转发
- 金典 SQL笔记(5)
- 160.Intersection of Two Linked Lists(两个单链表的相交结点)
- UML概念及其解释