Android动态修改Button背景色
先定义几种,注意为int[]类型
final int[] btn_backgrounds = new int[]{R.drawable.btn_shape,R.drawable.btn_shape1,R.drawable.btn_shape2,R.drawable.btn_shape3};
btn_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#017DDF"/>
<corners
android:topLeftRadius="8dip"
android:topRightRadius="8dip"
android:bottomLeftRadius="8dip"
android:bottomRightRadius="8dip"
>
</corners>
</shape>
btn_shape1.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#C6C6C6"/>
<corners
android:topLeftRadius="8dip"
android:topRightRadius="8dip"
android:bottomLeftRadius="8dip"
android:bottomRightRadius="8dip"
>
</corners>
</shape>
然后根据自己的条件,为button改变颜色
if(type == 0){
button.setBackgroundResource(R.drawable.btn_shape);
}
if(type == 1){
button.setBackgroundResource(R.drawable.btn_shape1);
}
即可实现根据 type的不同,使button的背景发生变化。