view在使用shape属性加圆角的同时,用代码修改其他background属性(例如颜色)不生效

项目中一个TextView控件设置了shape属性,给其加了圆角,如下:
1 houlder.mtxtGovernmentType.setBackgroundResource(R.drawable.tv_circular);
R.drawable.tv_circular的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />
    <solid android:color="#00000000" />
</shape>
一个view只能设置一个background,每设置一次Background,另一个Background就会就会被替换掉。
所以用下面这个GradientDrawable函数来进行多个设置
1 GradientDrawable myGrad = (GradientDrawable) houlder.mtxtGovernmentType.getBackground();
2 if (item.getStatus().equals("登记")) {
3      myGrad.setColor(context.getResources().getColor(R.color.theme_color_primary_light));
4 } else {
5      myGrad.setColor(context.getResources().getColor(R.color.zong));
6  }

 

 
posted @ 2017-04-18 17:42  陈炳序  阅读(946)  评论(0编辑  收藏  举报