How to draw rounded rectangle in Android UI?

前端 未结 8 859
北海茫月
北海茫月 2020-11-27 11:46

I need to draw a rounded rectangle in the Android UI. Having the same rounded rectangle for TextView and EditText would also be helpful.

相关标签:
8条回答
  • 2020-11-27 12:35

    In monodroid, you can do like this for rounded rectangle, and then keeping this as a parent class, editbox and other layout features can be added.

     class CustomeView : TextView
        {
           public CustomeView (Context context, IAttributeSet ) : base (context, attrs)  
            {  
            }
           public CustomeView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)  
            {  
            }
           protected override void OnDraw(Android.Graphics.Canvas canvas)
           {
               base.OnDraw(canvas);
               Paint p = new Paint();
               p.Color = Color.White;
               canvas.DrawColor(Color.DarkOrange);
    
               Rect rect = new Rect(0,0,3,3);
    
               RectF rectF = new RectF(rect);
    
    
               canvas.DrawRoundRect( rectF, 1,1, p);
    
    
    
           }  
        }
    }
    
    0 讨论(0)
  • 2020-11-27 12:37

    Right_click on the drawable and create new layout xml file in the name of for example button_background.xml. then copy and paste the following code. You can change it according your need.

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
    android:radius="14dp" />
    <solid android:color="@color/colorButton" />
    <padding
    android:bottom="0dp"
    android:left="0dp"
    android:right="0dp"
    android:top="0dp" />
    <size
    android:width="120dp"
    android:height="40dp" />
    </shape>
    

    Now you can use it.

    <Button
    android:background="@drawable/button_background"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
    
    0 讨论(0)
提交回复
热议问题