Android 代码里动态设置TextView/Button等的文字颜色Seletor
来源:互联网 发布:nodejs软件 编辑:程序博客网 时间:2024/05/01 06:01
- 前言
今天遇到个很蛋疼的问题,下载时,多个按钮共用一个button,也就是不同下载状态下,button的背景以及字体颜色都不一样,结果自己挖了坑把自己埋进去了。
以下是我在/res/color文件夹里给button设置的文字颜色seletor:networkdata_btn_open_txtcolor_selector.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#ffffff" android:state_pressed="true"></item> <item android:color="#ffffff" android:state_selected="true"></item> <item android:color="#f88b00"></item></selector>
坑?
在代码里设置颜色seletor,以为在代码里直接调用button.setTextColor(int colorValue)
就可以了,结果运行效果让我傻眼了。怎么挖坑的?
mBtnDownAndOpen.setText(DOWNLOAD_OPEN); //设置button文字 mBtnDownAndOpen.setTextColor(mContext.getResources().getColor(R.color.networkdata_btn_open_txtcolor_selector)); //设置button文字颜色 mBtnDownAndOpen.setBackground(mContext.getResources().getDrawable(R.drawable.networkdata_btn_open_selector)); //设置button背景
郁闷的是,只能读取到没获取焦点时的色值,也就是 <item android:color="#f88b00"></item>
,其他状态获取不到。
- 如何填坑?
采用mBtnDownAndOpen.setTextColor(mContext.getResources().getColorStateList(R.color.networkdata_btn_open_txtcolor_selector));
为button设置文字颜色。
区别在于:改之前用的是getColor,改之后用的是getColorStateList
- WHY?
文字颜色的seletor在代码里的显示形式是ColorStateList,而res/color放的就是ColorStateList资源XML文件,getColor只能读取单个的color。
浅薄理解,欢迎大家讨论!
3 0
- Android 代码里动态设置TextView/Button等的文字颜色Seletor
- android使用java代码设置textview、button等内容颜色
- Android 代码设置TextView、Button等控件圆角边框背景颜色
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
- Android布局中动态修改TextView等控件中文字的颜色
- Android 单个TextView中文字颜色,超链接等效果的设置
- Android 单个TextView中文字颜色,超链接等效果的设置
- Android 单个TextView中文字颜色,超链接等效果的设置
- Android TextView中文字通过SpannableString来设置不同的属性:超链接、颜色、字体等属性
- Android 在代码里动态设置ProgressBar的颜色
- Android中动态设置TextView的颜色
- Android中动态设置TextView的颜色
- 设置TextView文字的颜色
- Android代码生成TextView,Button,CheckBox等控件的设置技巧
- Android 代码设置TextView的大小颜色
- Android Button及TextView动态变换颜色
- Android Button及TextView动态变换颜色
- web安全
- Axure7.0自带的变量和函数有哪些?
- eclipse代码自动提示,eclipse设置代码自动提示
- TXAA
- memcache 分布式,算法实现
- Android 代码里动态设置TextView/Button等的文字颜色Seletor
- git 失效
- 在Linux(CentOS)下,AAPT解析APK指令报错少库
- iOS开发——生命周期
- UE4 iOS Packaged Game Size
- sqlite3 中执行带变量的命令
- 让scrollview滚动到最底部
- 三元组矩阵的乘法
- jquery实现当页面滚动超过一屏时显示返回顶部按钮