Android setBackGroundColor重置视图的形状
我想要有圆形Android setBackGroundColor重置视图的形状,android,xml,view,shape,Android,Xml,View,Shape,我想要有圆形TextViews,我通过定义一个工作良好的可绘制形状来实现这一点 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <solid android:color="@color/White"/> </shape> 但是它将文本视图形状重置为矩形 因此,问题总结为:动态更改视图颜色的正确方法是什么?它变成了一
TextView
s,我通过定义一个工作良好的可绘制形状来实现这一点
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="@color/White"/>
</shape>
但是它将文本视图形状重置为矩形强>
因此,问题总结为:动态更改视图颜色的正确方法是什么?它变成了一个矩形,因为您正在更改视图的背景,当然是矩形,您应该更改纯色,但我认为这不可能通过编程实现 唯一的办法是这样做:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="@color/Red"/>
</shape>
GradientDrawable drawable = (GradientDrawable)textView.getBackground();
drawable.setColor(getResources().getColor(android.R.color.darker_gray));
使用
setBackground(drawable);
而不是
setBackgroundColor(getResources().getColor(R.color.Red));
您可以通过以下操作以编程方式更改视图形状的背景:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="@color/Red"/>
</shape>
GradientDrawable drawable = (GradientDrawable)textView.getBackground();
drawable.setColor(getResources().getColor(android.R.color.darker_gray));
R.color.Red是您的自定义xml吗?@MysticsMagic:不,这是一个简单的颜色值,我自己也有这个值,但如果有许多颜色,您必须创建20个xml文件。这是违反干燥原则的!
GradientDrawable drawable = (GradientDrawable)textView.getBackground();
drawable.setColor(getResources().getColor(android.R.color.darker_gray));