How do I set the rounded corner radius of a color drawable using xml?

前端 未结 3 357
南方客
南方客 2020-12-02 07:45

On the android website, there is a section about color drawables. Defining these drawables in xml looks like this:


    

        
相关标签:
3条回答
  • 2020-12-02 08:13

    Try below code

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners
        android:bottomLeftRadius="30dp"
        android:bottomRightRadius="30dp"
        android:topLeftRadius="30dp"
        android:topRightRadius="30dp" />
    <solid android:color="#1271BB" />
    
    <stroke
        android:width="5dp"
        android:color="#1271BB" />
    
    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" /></shape>
    
    0 讨论(0)
  • 2020-12-02 08:23

    Use the <shape> tag to create a drawable in XML with rounded corners. (You can do other stuff with the shape tag like define a color gradient as well).

    Here's a copy of a XML file I'm using in one of my apps to create a drawable with a white background, black border and rounded corners:

    <?xml version="1.0" encoding="UTF-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <solid android:color="#ffffffff"/>    
                 
        <stroke android:width="3dp"
                android:color="#ff000000" />
    
        <padding android:left="1dp"
                 android:top="1dp"
                 android:right="1dp"
                 android:bottom="1dp" /> 
                 
        <corners android:radius="7dp" /> 
    </shape>
    
    0 讨论(0)
  • 2020-12-02 08:38

    mbaird's answer works fine. Just be aware that there seems to be a bug in Android (2.1 at least), that if you set any individual corner's radius to 0, it forces all the corners to 0 (at least that's the case with "dp" units; I didn't try it with any other units).

    I needed a shape where the top corners were rounded and the bottom corners were square. I got achieved this by setting the corners I wanted to be square to a value slightly larger than 0: 0.1dp. This still renders as square corners, but it doesn't force the other corners to be 0 radius.

    0 讨论(0)
提交回复
热议问题