以编程方式更改一个属性,而不是创建一个新的(几乎相同)xml文件

时间:2018-12-18 20:55:34

标签: java android xml android-styles

我想知道是否有办法更改样式属性之一,例如:如果我有两种颜色和一个可绘制的xml文件,则以编程方式更改backgroundColor。 换句话说:如果我的每个组件都使用一个xml可绘制文件但颜色不同,该怎么办?

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="#00E100" /> //i have second with #FFFFFF color
        <stroke android:color="#8500FF"/>
        <stroke android:width="1dp"/>
        <corners android:radius="2dp"/>
    </shape>
</item>
</layer-list>

和一些代码:

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    holder.final_list_TextView1.setText(finalListItems.get(position).getName());
//just want set one xml style here: like setBackgroundResource()...
    holder.checkBox.setOnCheckedChangeListener(null);
    holder.checkBox.setBackgroundResource(R.drawable.listitem_white);
    if(finalListItems.get(position).isChecked){
        holder.linearLayout.setBackgroundResource(R.drawable.listitem_green); 
//and here just change only color of this style
        holder.checkBox.setChecked(true);
    }
    else{
        holder.linearLayout.setBackgroundResource(R.drawable.listitem_white);
        holder.checkBox.setChecked(false);
    }

0 个答案:

没有答案