Issue while setting background resource for second time

时间:2017-10-12 09:51:32

标签: android

I am facing issue while setting background.

I have the condition like below:-

if (!TextUtils.isEmpty(selectedRgbArray[3].getCode())) {
    color_3.setBackgroundColor(Color.parseColor(selectedRgbArray[3].getValue()));
} else {
    color_3.setBackgroundResource(R.drawable.bg_palette);
}        

In the above condition by default compiler goes in else condition but after a button click it again goes in second condition, for first time it works perfectly but second time it sets gray background which cannot be as bg_palette is a gray border drawable.

bg_palette.xml is below:-

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" />
    <stroke
        android:width="@dimen/size_01"
        android:color="@color/light_grey" />
    <corners android:radius="0dip" />

</shape>

Please help me getting my mistake over there. Thanks in advance.

2 个答案:

答案 0 :(得分:0)

Try this in your code .

1.add color to the colors

<color name="color_white">#FFFFFF</color>

2.set it to solid

<solid android:color="@color/color_white" />

EDIT

Add android:shape="rectangle" of shape xml code

<shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle">

And you can change to other shape .

答案 1 :(得分:0)

感谢大家通过将滤镜设置为背景

找到了解决方案
if (!TextUtils.isEmpty(selectedRgbArray[3].getCode())) {
     color_1.getBackground().setColorFilter(Color.parseColor(selectedRgbArray[1].getValue()), PorterDuff.Mode.DARKEN);
} else {
    color_1.getBackground().setColorFilter(Color.TRANSPARENT, PorterDuff.Mode.DARKEN);
} 

我已经通过色调设置背景并且它有效。