我想在运行时使一个视图的角变圆,并根据内容改变视图的颜色。
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);
我希望设置一个可绘制对象和颜色可以重叠,但实际上并不会。无论最后执行哪个,它都会成为背景。
有没有办法以编程方式创建这个视图,同时记住背景颜色在运行时才能确定?
编辑:目前我只是为了测试在红色和蓝色之间切换。稍后用户将能够选择颜色。
编辑:
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>
tags_rounded_corners
是什么? - Alex MDC