Tengo una vista personalizada donde tengo que cambiar el color de la forma de fondo del código según la entrada.
La forma:
<?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>La vista personalizada:
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); } } } El color de la forma siempre #BBBBBB . No puedo cambiarlo, por ejemplo, a red , independientemente del atributo relacionado con el color que pruebe. Tengo que cambiar el color según la entrada.
Puede establecer un filtro de color para su forma dibujable:
Drawable background = getContext().getResources().getDrawable(R.drawable.shape_background); background.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN); this.setBackground(background);Puede cambiar el color usando 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>en 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);Para obtener más información, consulte el siguiente enlace,
Pruebe el siguiente código, coloque este código en su clase de vista de texto personalizada y cambie el color de fondo según sus requisitos.
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);