How to make the corners of a button round?

前端 未结 13 795
暖寄归人
暖寄归人 2020-11-22 16:36

I want to make the corners of a button round. Is there an easy way to achieve this in Android?

相关标签:
13条回答
  • 2020-11-22 17:15

    Create file myButton.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@color/colorButton"/>
        <corners android:radius="10dp"/>
    </shape>
    

    add to your button

     <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/myButton"/>
    
    0 讨论(0)
  • 2020-11-22 17:16

    create shape.xml in drawable folder

    like shape.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
      <stroke android:width="2dp"
        android:color="#FFFFFF"/>
      <gradient 
        android:angle="225"
        android:startColor="#DD2ECCFA"
        android:endColor="#DD000000"/>
    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
       android:topRightRadius="7dp" />
    </shape>
    

    and in myactivity.xml

    you can use

    <Button
        android:id="@+id/btn_Shap"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="@string/Shape"
        android:background="@drawable/shape"/>
    
    0 讨论(0)
  • 2020-11-22 17:17

    Create an XML file like below one. Set it as background for the button. Change the radius attribute to your wish, if you need more curve for the button.

    button_background.xml

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

    Set background to your button:

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_background"/>
    
    0 讨论(0)
  • 2020-11-22 17:27

    Create a xml file in drawable folder like below

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" android:padding="10dp">
        <!-- you can use any color you want I used here gray color-->
        <solid android:color="#ABABAB"/> 
        <corners android:radius="10dp"/>
    </shape>
    

    Apply this as background to button you want make corners round.

    Or you can use separate radius for every corner like below

    android:bottomRightRadius="10dp"
    android:bottomLeftRadius="10dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp"
    
    0 讨论(0)
  • 2020-11-22 17:29

    if you are using vector drawables, then you simply need to specify a <corners> element in your drawable definition. I have covered this in a blog post.

    If you are using bitmap / 9-patch drawables then you'll need to create the corners with transparency in the bitmap image.

    0 讨论(0)
  • 2020-11-22 17:30

    This link has all the information you need. Here

    Shape.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <shape      xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle">
    
        <solid   android:color="#EAEAEA"/>
    
        <corners    android:bottomLeftRadius="8dip"
                    android:topRightRadius="8dip"
                    android:topLeftRadius="1dip"
                    android:bottomRightRadius="1dip"
                    />
    </shape>
    

    and main.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent">
    
        <TextView   android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Hello Android from NetBeans"/>
    
        <Button android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Nishant Nair"
                android:padding="5dip"
                android:layout_gravity="center"
                android:background="@drawable/button_shape"
                />
    </LinearLayout>
    

    This should give you your desired result.

    Best of luck

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