首页 文章

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

提问于
浏览
98

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

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

我希望设置一个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>

5 回答

  • 5

    而不是 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);
    }
    

    此外,您可以在 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>
    
  • 176

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

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

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

  • 103

    我认为最快的方法是:

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

    您可以使用_2651668这样更好地实现它:

    Drawable backgroundDrawable = view.getBackground();             
    DrawableCompat.setTint(backgroundDrawable, newColor);
    
  • 8

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

    colorDrawable = resources.getDrawable(R.drawable.x_sd_circle); 
    
    colorDrawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    

    但这也会改变笔画颜色

相关问题