How to programmatically round corners and set random background colors

后端 未结 8 1713
眼角桃花
眼角桃花 2020-11-29 17:50

I\'d like to round the corners of a view and also change the color of the view based on the contents at runtime.

TextView v = new TextView(context);
v.setTex         


        
8条回答
  •  时光说笑
    2020-11-29 18:42

    I think the fastest way to do this is:

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

提交回复
热议问题