Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何更改文本视图';什么是背景色?_Android_Textview_Background Drawable - Fatal编程技术网

Android 如何更改文本视图';什么是背景色?

Android 如何更改文本视图';什么是背景色?,android,textview,background-drawable,Android,Textview,Background Drawable,我使用circle.xml(在res/drawable中)引用并实现了TextView的循环背景,并将其设置为android:background=“@drawable/circle”,用于TextView。但我需要的是,我需要通过代码动态设置背景色。就像棒棒糖联系人应用程序一样,如下所示 我怎样才能做到这一点?我需要始终如上图所示的圆形文本视图背景您可以通过多种方式更改文本视图背景颜色,如: textView.setBackgroundColor(Color.parseColor("#f44

我使用
circle.xml
(在res/drawable中)引用并实现了TextView的循环背景,并将其设置为
android:background=“@drawable/circle”
,用于TextView。但我需要的是,我需要通过代码动态设置背景色。就像棒棒糖联系人应用程序一样,如下所示


我怎样才能做到这一点?我需要始终如上图所示的圆形文本视图背景

您可以通过多种方式更改文本视图背景颜色,如:

textView.setBackgroundColor(Color.parseColor("#f44336"));

还有很多其他的方法

编辑:

如果要更改可绘制文件中定义的TextView背景色,请执行以下操作:

渐变可绘制:

StateListDrawable:


但是,如果您不想设置颜色过滤器,您可以按照本文中的答案分别获得每个状态的可绘制颜色。

我想您想问一下如何生成随机颜色以设置为textview背景。嗯,有很多方法。e、 g

textview.setBackgroundColor(Color.rgb((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)));

“我的文字”视图的圆形定义为

//circleshape.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="schemas.android.com/apk/res/android"; android:shape="oval"> 
<solid android:color="@android:color/darker_gray" /> 
<corners android:bottomRightRadius="8dp" android:bottomLeftRadius="8dp" android:topRightRadius="8dp" android:topLeftRadius="8dp"/> 
</shape>

是的,所有这些都是可以用来达到预期效果的选项。我只是想加上其余的。但这并没有给我圆形的背景形状。它只改变背景色。圆形背景将消失,新颜色将显示在方形背景中。您在问题中没有提到您希望圆形。GradientDrawable不适用于TextView。获取错误:java.lang.ClassCastException:android.graphics.drawable.StateListDrawable无法强制转换为android.graphics.drawable。GradientDrawable@drod似乎您正在使用StateListDrawable而不是GradientDrawable。StateListDrawable可以有许多背景状态,因此没有明确的背景。试试我最新的答案。但这不适用于生成随机颜色<代码>新颜色((int)(Math.random()*0x1000000))此语句无效
textView.setBackgroundColor(getColor(R.color.red_color));
GradientDrawable tvBackground = (GradientDrawable) textView.getBackground();
tvBackground.setColor(Color.parseColor("#f44336"));
StateListDrawable tvBackground = (StateListDrawable) textView.getBackground();
tvBackground.setColorFilter(Color.parseColor("#f44336"), PorterDuff.Mode.SRC_ATOP);
textview.setBackgroundColor(Color.rgb((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)));
<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="schemas.android.com/apk/res/android"; android:shape="oval"> 
<solid android:color="@android:color/darker_gray" /> 
<corners android:bottomRightRadius="8dp" android:bottomLeftRadius="8dp" android:topRightRadius="8dp" android:topLeftRadius="8dp"/> 
</shape>
GradientDrawable tvBackground = (GradientDrawable) viewHolder.userInitialsText.getBackground();

//myHexColorCode  is like "0xff00ff"
tvBackground.setColor(Color.parseColor(myHexColorCode));