Android 代码里动态设置TextView/Button等的文字颜色Seletor

 

 //设置文本颜色
     btn.setTextColor(getResources().getColorStateList(R.color.find_tab_btn_selector));
//设置背景
     btn.setBackground(this.getResources().getDrawable(R.drawable.selector_tab_txt_bottom));
//设置底边图片
     Drawable drawable = getResources().getDrawable(R.drawable.selector_tab_txt_bottom);
     // 这一步必须要做,否则不会显示.
     drawable.setBounds(0, 0, drawable.getMinimumWidth(), 5);
     btn.setCompoundDrawables(null, null, null, drawable);
 以下是我在/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>
 
 mBtnDownAndOpen.setText(DOWNLOAD_OPEN);  //设置button文字
 mBtnDownAndOpen.setTextColor(mContext.getResources().getColorStateList(R.color.networkdata_btn_open_txtcolor_selector));   //设置button文字颜色
 mBtnDownAndOpen.setBackground(mContext.getResources().getDrawable(R.drawable.networkdata_btn_open_selector));    //设置button背景
  文字颜色的seletor在代码里的显示形式是ColorStateList,而res/color放的就是ColorStateList资源XML文件,getColor只能读取单个的color。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值