83

我想要圆角视图,并根据运行时的内容更改视图的颜色。如何以编程方式围绕角落并设置随机背景颜色

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> 
+0

当然背景颜色和背景图像相互覆盖。你想达到什么目的?什么是'tags_rounded_corners'? –

+0

你能展示更多代码吗?它看起来很好,所以我想知道你可能会使用一种listView,或者重用现有的textview。 – Chansuk

+0

请查看http://www.gadgetsaint.com/tips/rounded-corners-views-layouts-android/#.WPz2QVN97BI – ASP

回答

148

代替setBackgroundColor,检索背景绘制并设置其颜色:

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); 
} 

另外,还可以中定义的填充你的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> 
+0

Perect answe!它也与strock一起工作,但是我们可以使用类似 的颜色colorDrawable = resources.getDrawable(R.drawable.x_sd_circle); colorDrawable.setColorFilter(color,PorterDuff.Mode.SRC_ATOP); 如果我们没有边框。 但在边框的情况下,你可以让我知道PorterDuff.Mode,以便笔迹颜色不会改变 –

+0

如何通过XML添加背景颜色? – emaillenin

+2

如果“v”是TextView而不是v.getBackground()会投射“java.lang.ClassCastException:android.graphics.drawable.StateListDrawable不能转换为android.graphics.drawable.GradientDrawable”这是真的在'13 ? – sonavolob

86

用于设置圆角并将随机背景颜色添加到视图的总体编程方法。 我没有测试过代码,但你明白了。

GradientDrawable shape = new GradientDrawable(); 
shape.setCornerRadius(8); 

// add some color 
// You can add your random color generator here 
// and set color 
if (i % 2 == 0) { 
    shape.setColor(Color.RED); 
} else { 
    shape.setColor(Color.BLUE); 
} 

// now find your view and add background to it 
View view = (LinearLayout) findViewById(R.id.my_view); 
view.setBackground(shape); 

这里我们使用梯度绘制,使我们可以利用GradientDrawable#setCornerRadius因为ShapeDrawable不提供任何这样的方法。

+11

shape.setCornerRadii(corners);它非常有用 – umesh

+0

超级回答@jayadeepw –

+7

考虑使用'PaintDrawable'而不是'GradientDrawable'。它支持圆角和只是一种颜色,似乎比渐变更合适。 – Cimlman

2

如果你没有一个行程,你可以使用

colorDrawable = resources.getDrawable(R.drawable.x_sd_circle); 

colorDrawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP); 

但是这也将改变笔触颜色

+23

我打算使用这个,但我有中风。 –

7

我觉得这样做的最快方法是:

GradientDrawable gradientDrawable = new GradientDrawable(
      GradientDrawable.Orientation.TOP_BOTTOM, //set a gradient direction 
      new int[] {0xFF757775,0xFF151515}); //set the color of gradient 
gradientDrawable.setCornerRadius(10f); //set corner radius 

//Apply background to your view 
View view = (RelativeLayout) findViewById(R.id.my_view); 
if(Build.VERSION.SDK_INT>=16) 
    view.setBackground(gradientDrawable); 
else view.setBackgroundDrawable(gradientDrawable);  
0

你可以通过使用如下的DrawableCompat更好地实现它:

Drawable backgroundDrawable = view.getBackground();    
DrawableCompat.setTint(backgroundDrawable, newColor); 
相关问题