Android动态修改Button背景色

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的背景发生变化。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值