android样式学习(一) 使用selector改变来动态改变背景颜色

1、现在res/drawable目录下见mystyle.xml文件,内容如下:

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

 

2、在控件中引用:android:background="@drawable/mainstyle"

 <RadioButton 
            android:id="@+id/second"
            
            style="@style/main_style " 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:drawableTop="@drawable/group_add_checkbox_select"
            android:background="@drawable/mainstyle"
            android:text="第二个" /> 

3、颜色xml定义如下:

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="qianhuise">#F0F0F0</color>
    <color name="red">#fd8d8d</color>
 <color name="green">#9cfda3</color>
 <color name="blue">#8d9dfd</color>
  <color name="white">#FFFFFF</color>
  <color name="orange">#f37301</color>
 <color name="black">#000000</color>
 <color name="gray">#050505</color>
  <color name="listDivider">#ffcc99</color>
</resources>

你可能感兴趣的:(android样式学习(一) 使用selector改变来动态改变背景颜色)