Rounded corner for textview in android

前端 未结 11 1138
猫巷女王i
猫巷女王i 2020-12-02 04:30

I have a textview and want its corner to be in round shape. I already know it can be done using android:background=\"@drawable/somefile\". In my case, this tag

相关标签:
11条回答
  • 2020-12-02 04:49

    Simply using an rounded corner image as the background of that view

    And don't forget to have your custom image in drawable folder

    android:background="@drawable/my_custom_image"
    
    0 讨论(0)
  • 2020-12-02 04:50
    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:id="@android:id/background">
            <shape>
                <corners android:radius="5dp" />
                <solid android:color="#ffffff"/>
    
            </shape>
        </item>
    </layer-list>
    
    0 讨论(0)
  • 2020-12-02 05:01

    Since your top level view already has android:background property set, you can use a <layer-list> (link) to create a new XML drawable that combines both your old background and your new rounded corners background.

    Each <item> element in the list is drawn over the next, so the last item in the list is the one that ends up on top.

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list
        xmlns:android="http://schemas.android.com/apk/res/android" >
        <item>
            <bitmap android:src="@drawable/mydialogbox" />
        </item>
        <item>
            <shape>
                <stroke
                    android:width="1dp"
                    android:color="@color/common_border_color" />
    
                <solid android:color="#ffffff" />
    
                <padding
                        android:left="1dp"
                        android:right="1dp"
                        android:top="1dp" />
    
                <corners android:radius="5dp" />
            </shape>
        </item>
    </layer-list>
    
    0 讨论(0)
  • 2020-12-02 05:01

    You can use the provided rectangle shape (without a gradient, unless you want one) as follows:

    In drawable/rounded_rectangle.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <corners android:radius="5dp" />
        <stroke android:width="1dp" android:color="#ff0000" />
        <solid android:color="#00ff00" />
    </shape>
    

    Then in your text view:

    android:background="@drawable/rounded_rectangle"
    

    Of course, you will want to customize the dimensions and colors.

    0 讨论(0)
  • 2020-12-02 05:06
    1. Right Click on Drawable Folder and Create new File
    2. Name the file according to you and add the extension as .xml.
    3. Add the following code in the file
      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <corners android:radius="5dp" />
          <stroke android:width="1dp"  />
          <solid android:color="#1e90ff" />
      </shape>
    
    1. Add the line where you want the rounded edge android:background="@drawable/corner"
    0 讨论(0)
提交回复
热议问题