Android学习 之 ColorStateList按钮文字变色

 

首先添加一个ColorStateList资源XML文件,XML文件保存在res/color/button_text.xml:

Java代码  Android学习 之 ColorStateList按钮文字变色
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_pressed="true"  android:color="#ffff0000"/> 
  4.     <item android:state_focused="true"  android:color="#ff0000ff"/> 
  5.     <item android:color="#ff000000"/> 
  6. </selector>  

 

Java代码  Android学习 之 ColorStateList按钮文字变色
  1. Button btn=(Button)findViewById(R.id.btn);  
  2. Resources resource=(Resources)getBaseContext().getResources();   
  3. ColorStateList csl=(ColorStateList)resource.getColorStateList(R.color.button_text);  
  4. btn.setTextColor(csl);//设置按钮文字颜色  

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-31
  • 2021-08-15
  • 2022-01-06
  • 2021-06-11
猜你喜欢
  • 2022-01-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案