How to draw rounded rectangle in Android UI?

前端 未结 8 860
北海茫月
北海茫月 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:20

    In your layout xml do the following:

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <solid android:color="@android:color/holo_red_dark" />
    
        <corners android:radius="32dp" />
    
    </shape>
    

    By changing the android:radius you can change the amount of "radius" of the corners.

    <solid> is used to define the color of the drawable.

    You can use replace android:radius with android:bottomLeftRadius, android:bottomRightRadius, android:topLeftRadius and android:topRightRadius to define radius for each corner.

    0 讨论(0)
  • 2020-11-27 12:20

    I think, this is you exactly needed.

    Here drawable(xml) file that creates rounded rectangle. round_rect_shape.xml

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
    
        <solid android:color="#ffffff" />
    
        <corners
            android:bottomLeftRadius="8dp"
            android:bottomRightRadius="8dp"
            android:topLeftRadius="8dp"
            android:topRightRadius="8dp" />
    
    </shape>
    

    Here layout file: my_layout.xml

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/round_rect_shape"
        android:orientation="vertical"
        android:padding="5dp" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Something text"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ff0000" />
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <requestFocus />
        </EditText>
    </LinearLayout>
    

    -> In the above code, LinearLayout having the background(That is the key role to place to create rounded rectangle). So you can place any view like TextView, EditText... in that LinearLayout to see background as round rectangle for all.

    0 讨论(0)
  • 2020-11-27 12:25

    You could just define a new xml background in the drawables folder

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="enter_your_desired_color_here" />
    <corners android:radius="enter_your_desired_radius_the_corners" />
    </shape>  
    

    After this just include it in your TextView or EditText by defining it in the background.

    <TextView
     android:id="@+id/textView"
     android:layout_width="0dp"
     android:layout_height="80dp"
     android:background="YOUR_FILE_HERE"
     Android:layout_weight="1"
     android:gravity="center"
     android:text="TEXT_HERE"
     android:textSize="40sp" />
    
    0 讨论(0)
  • 2020-11-27 12:26
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:padding="10dp"
      android:shape="rectangle">
        <solid android:color="@color/colorAccent" /> 
        <corners
          android:bottomLeftRadius="500dp"
          android:bottomRightRadius="500dp"
          android:topLeftRadius="500dp"
          android:topRightRadius="500dp" />
    </shape>
    

    Now, in which element you want to use this shape just add: android:background="@drawable/custom_round_ui_shape"

    Create a new XML in drawable named "custom_round_ui_shape"

    0 讨论(0)
  • 2020-11-27 12:26

    Use CardView for Round Rectangle. CardView give more functionality like cardCornerRadius, cardBackgroundColor, cardElevation & many more. CardView make UI more suitable then Custom Round Rectangle drawable.

    0 讨论(0)
  • 2020-11-27 12:33
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="@android:color/white" />
        <corners android:radius="4dp" />
    </shape>
    
    0 讨论(0)
提交回复
热议问题