如何与Android上的形状属性更改的TextView的颜色在运行时? [英] How to change the color of TextView at runtime with shape attribute on Android?
本文介绍了如何与Android上的形状属性更改的TextView的颜色在运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用这样的形状属性:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:padding="10dp">
<solid
android:color="#FFFFFF" />
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
</shape>
和
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_textview">
</TextView>
如果我改变颜色在运行时用下面的方法:
If I change the color at runtime with the following method:
TextView.setBackgroundColor();
我使用的形状消失。我应该怎么做才能用正确的方式改变呢? 或者我应该必须能够产生大量的形状只是颜色不同?
The shape I used is disappear. What should I do to change it with the proper way? Or should I must have to generate lots of shape for just different colors?
感谢。
推荐答案
我发现PaintDrawable的解决方案,其中包含的颜色和RADIUS属性。 但它必须设置在构造器的颜色。所以,我有新的一个PaintDrawable在运行时每次并将其设置为一个TextView的背景绘制。
I found a solution with PaintDrawable which contains color and radius attributes. But It have to set the color in the contructor. So I have to new a PaintDrawable at runtime every time and set it to the background drawable of a TextView.
public static PaintDrawable getRoundedColorDrawable(int color, float radius, int padding) {
PaintDrawable paintDrawable = new PaintDrawable(color);
paintDrawable.setCornerRadius(radius);
paintDrawable.setPadding(padding, padding, padding, padding);
return paintDrawable;
}
这篇关于如何与Android上的形状属性更改的TextView的颜色在运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文