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

Joh*_*itt 105 android background rounded-corners android-layout android-view

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

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)

chi*_*uki 195

而不是setBackgroundColor,检索背景drawable并设置其颜色:

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

此外,您可以在以下内容中定义填充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> 
Run Code Online (Sandbox Code Playgroud)

  • 如果"v"是TextView而不是v.getBackground()将转换为"java.lang.ClassCastException:android.graphics.drawable.StateListDrawable无法强制转换为android.graphics.drawable.GradientDrawable"这是否真的在13年回归? (2认同)

Jay*_*epW 114

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

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

这里我们使用渐变drawable,以便我们可以使用,GradientDrawable#setCornerRadius因为ShapeDrawable不提供任何这样的方法.

  • shape.setCornerRadii(角部); 它非常有用 (12认同)
  • 考虑使用`PaintDrawable`而不是`GradientDrawable`.它支持圆角和单一颜色,似乎比渐变更合适. (12认同)
  • 这很好用!我在Xamarin中使用它.`var pd = new PaintDrawable(BackgroundColor); pd.SetCornerRadius(15); myView.Background = pd;` (2认同)

Nol*_*esh 9

我认为最快的方法是:

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


Alé*_*lho 8

您可以通过使用DrawableCompat更好地实现它:

Drawable backgroundDrawable = view.getBackground();             
DrawableCompat.setTint(backgroundDrawable, newColor);
Run Code Online (Sandbox Code Playgroud)


小智 8

由于问题已经得到解答。但我有一点调整

GradientDrawable drawable = (GradientDrawable) ContextCompat.getDrawable(context, R.drawable.YOUR_DRAWABLE).mutate();
Run Code Online (Sandbox Code Playgroud)

您可以通过以下方式更改圆角半径:

drawable.setCornerRadius(YOUR_VALUE);
Run Code Online (Sandbox Code Playgroud)

改变颜色:

drawable.setColor(Color.YOUR_COLOR);
Run Code Online (Sandbox Code Playgroud)

保证可变可绘制对象不会与任何其他可绘制对象共享其状态。因此,如果您在不使用 mutate() 的情况下更改半径,您也可能会更改其他状态。它最适合特定的视图。

最后不要忘记设置可绘制对象。

this.setBackground(drawable);
Run Code Online (Sandbox Code Playgroud)


Akh*_*Dad 5

如果您没有中风,您可以使用

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

colorDrawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
Run Code Online (Sandbox Code Playgroud)

但这也会改变笔画颜色

  • 我打算用这个,但我中风了. (37认同)