如何以编程方式圆角和随机设置背景颜色 [英] How to programmatically round corners and set random background colors
问题描述
我想圆一个视图的角落,改变的基础上,在运行时的内容的视图的颜色。
TextView的V =新的TextView(上下文);
v.setText(tagsList.get(ⅰ));
如果(我%2 == 0){
v.setBackgroundColor(Color.RED);
}
其他{
v.setBackgroundColor(Color.BLUE);
}
v.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
v.setPadding(twoDP,twoDP,twoDP,twoDP);
v.setBackgroundResource(R.drawable.tags_rounded_corners);
我希望设置一个绘制和颜色会重叠,但他们没有。哪一个我执行第二个则产生的背景。
有没有办法以编程方式创建这个观点,牢记背景颜色不会决定,直到运行时?
编辑:我只是在红色和蓝色的,现在换了测试。后来颜色会可选择的用户。
编辑:
tags_rounded_corners.xml:
< XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=http://schemas.android.com/apk/res/android>
<角落
机器人:bottomRightRadius =2DP
机器人:bottomLeftRadius =2DP
机器人:topLeftRadius =2DP
机器人:topRightRadius =2DP/>
< /形状>
而不是 setBackgroundColor
中,检索背景绘制并设置它的颜色:
v.setBackgroundResource(R.drawable.tags_rounded_corners);
GradientDrawable绘制=(GradientDrawable)v.getBackground();
如果(我%2 == 0){
drawable.setColor(Color.RED);
} 其他 {
drawable.setColor(Color.BLUE);
}
此外,您还可以在定义填充你的 tags_rounded_corners.xml
:
< XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=http://schemas.android.com/apk/res/android>
<边角机器人:半径=4DP/>
<填充
机器人:顶部=2DP
机器人:左=2DP
机器人:底部=2DP
机器人:右=2DP/>
< /形状>
I'd like to round the corners of a view and also change the color of the view based on the contents at runtime.
TextView v = new TextView(context);
v.setText(tagsList.get(i));
if(i%2 == 0){
v.setBackgroundColor(Color.RED);
}
else{
v.setBackgroundColor(Color.BLUE);
}
v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
v.setPadding(twoDP, twoDP, twoDP, twoDP);
v.setBackgroundResource(R.drawable.tags_rounded_corners);
I was hoping setting a drawable and color would overlap, but they do not. Whichever one I execute second is the resulting background.
Is there any way to programmatically create this view, keeping in mind that the background color won't be decided until runtime?
edit: I'm only swapping between red and blue now for testing. Later the color will be choosable by the user.
edit:
tags_rounded_corners.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners
android:bottomRightRadius="2dp"
android:bottomLeftRadius="2dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp"/>
</shape>
Instead of setBackgroundColor
, retrieve the background drawable and set its color:
v.setBackgroundResource(R.drawable.tags_rounded_corners);
GradientDrawable drawable = (GradientDrawable) v.getBackground();
if (i % 2 == 0) {
drawable.setColor(Color.RED);
} else {
drawable.setColor(Color.BLUE);
}
Also, you can define the padding within your tags_rounded_corners.xml
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="4dp" />
<padding
android:top="2dp"
android:left="2dp"
android:bottom="2dp"
android:right="2dp" />
</shape>
这篇关于如何以编程方式圆角和随机设置背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!