相关疑难解决方法(0)

如何以编程方式圆角和设置随机背景颜色

我想围绕视图的角落,并在运行时根据内容更改视图的颜色.

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);
Run Code Online (Sandbox Code Playgroud)

我希望设置一个drawable,颜色会重叠,但他们没有.无论我执行第二个是生成的背景.

有没有办法以编程方式创建此视图,请记住,直到运行时才会确定背景颜色?

编辑:我现在只在红色和蓝色之间进行测试.之后,用户可以选择颜色.

编辑:

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>
Run Code Online (Sandbox Code Playgroud)

android background rounded-corners android-layout android-view

105
推荐指数
6
解决办法
10万
查看次数