android:带有圆形背景和动态背景颜色的TextView

时间:2017-06-12 13:58:21

标签: android


我需要使用带有动态颜色的圆形背景制作TextView 我知道如何制作可绘制的背景但我不知道如何在代码中更改它的颜色?

drawable bg xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/colorPrimary"></solid>
            <corners android:radius="7dp"></corners>
        </shape>
    </item>


</selector>


布局xml中的textview:

<TextView
            android:id="@+id/txt_taskTag"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/bg_rounded_solid"
            android:paddingEnd="10dp"
            android:paddingStart="10dp"
            android:text="عمل"
            android:textColor="#fff"
            android:layout_marginEnd="10dp"
            android:textSize="12sp" />

在Java文件代码中:

 public void onBindViewHolder(final ViewHolder holder, int position) {  

       holder.txt_taskCategory.setText(holder.mTask._catName);
       holder.txt_taskCategory.setBackgroundColor( Color.parseColor( holder.mTask._catColor));
    //when i do that it remove the drawable background and just color it.


}

我需要的是改变可绘制背景(而不是Textview)的颜色"holder.mTask._catColor"

2 个答案:

答案 0 :(得分:1)

v.setBackgroundColor(0xFF00FF00);

尝试这个(其中0xFF00FF00是颜色代码)来改变java代码中的颜色,并在其中更改它的条件 如果你想在一段时间后改变你可以设置计时器或使用gif

答案 1 :(得分:0)

这段代码对我有用

    Drawable drawable = getResources().getDrawable(R.drawable.yourViewBackground);
    drawable.mutate().setColorFilter(your_resolved_color, PorterDuff.Mode.SRC_IN);
    yourView.setBackground(drawable);
相关问题