我有一个名为layout_bg_exp.xml的drawable,其代码为
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/ColorNecessities" android:id="@+id/listExpensesBgColor" />
<corners android:radius="5dip"/>
</shape>
使用以下代码和color.xml(在values文件夹下):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ColorNecessities">#eac49a</color>
<color name="ColorSavings">#a5e1f7</color>
<color name="ColorEntertainment">#f1f277</color>
<color name="ColorInvestment">#a6e9a3</color>
<color name="ColorEducation">#c2f7ee</color>
<color name="ColorCharity">#f7c2f7</color>
</resources>
layout_bg_exp在我的布局文件list_expenses.xml中引用,如下所述(截断只是为了显示引用layout_bg_exp的部分):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal" android:layout_width="fill_parent"
android:id="@+id/llBoxDescription"
android:layout_height="fill_parent"
android:background="@drawable/layout_bg_exp">
</LinearLayout>
现在我的list_expenses.xml被我的RecyclerView适配器膨胀,如下所示:
@Override
public AdapterExpensesList.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_expenses, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
所以我的问题是,无论如何我可以以编程方式将listExpensesBgColor的值从ColorNecessities更改为其他(可能是ColorCharity或ColorSavings等)吗?实际上我想要实现的是,我想根据RecyclerView中的项目更改颜色。
答案 0 :(得分:1)
我认为这样的事情应该有效:
GradientDrawable GradientDrawable = (GradientDrawable)v.getBackground();
GradientDrawable .setColor(YOR_DESIRE_COLOR);
答案 1 :(得分:0)
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_expenses, parent, false);
if(item==<your desired item>)
{
v.setBackgroundColor(<YourDesiredColor>);
}
else if(condition
{
.
.
.
so on