如何以编程方式更改背景形状的颜色

时间:2017-04-29 12:32:12

标签: java android android-custom-view

我有一个自定义视图,我必须根据输入从代码中更改背景形状的颜色。

形状:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size
        android:width="64dp"
        android:height="64dp" />
    <solid
        android:color="#BBBBBB" />
</shape>

自定义视图:

public class MyCustomView extends TextView {

    public MyCustomView(Context context) {
        super(context);
    }

    public MyCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setCustomValue(long customValue) {
        this.setBackgroundResource(R.drawable.shape_background);
        this.setTextColor(getColor(R.color.colorCustomViewText));
        this.setGravity(Gravity.CENTER);
        this.setText(String.valueOf(customValue));
    }

    public long getCustomValue() {
        return Long.parseLong(this.getText().toString());
    }

    private int getColor(int colorId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return getContext().getColor(colorId);
        } else {
            return getContext().getResources().getColor(colorId);
        }
    }
}

形状的颜色始终为#BBBBBB。我无法将其更改为例如red无论我尝试哪种颜色相关属性。我必须根据输入改变颜色。

3 个答案:

答案 0 :(得分:2)

您可以使用GradientDrawable Api

更改颜色

XML,

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/outerRectangle">
    <shape android:shape="oval" >
        <solid android:color="#FCD366" />

        <stroke
            android:width="1dp"
            android:color="@android:color/darker_gray" />
    </shape>
</item>

在Java中,

  Drawable tempDrawable = getResources().getDrawable(R.drawable.library_cirecle);
LayerDrawable bubble = (LayerDrawable) tempDrawable; (cast to root element in xml)
    GradientDrawable solidColor = (GradientDrawable) bubble.findDrawableByLayerId(R.id.outerRectangle);
    solidColor.setColor(newColorCode);
    imageView.setImageDrawable(tempDrawable);

有关详细信息,请参阅以下链接

Link1 Link2

答案 1 :(得分:1)

您可以将颜色过滤器设置为可绘制的形状:

Drawable background = getContext().getResources().getDrawable(R.drawable.shape_background);
background.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
this.setBackground(background);

答案 2 :(得分:1)

尝试下面的代码,将此代码放在customtextview类中,并根据您的要求更改背景颜色。

GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{ContextCompat.getColor(this,R.color.red_500),
            ContextCompat.getColor(this,R.color.red_500), ContextCompat.getColor(this,R.color.red_500)});
    gd.setShape(GradientDrawable.RECTANGLE);
    gd.setStroke((int)0.5, ContextCompat.getColor(this, R.color.black));
    gd.setCornerRadius(8f);
    gd.setBounds(2, 2, 2, 2);
    btnCreateUser.setBackground(gd);

Here is a screenshot