How to make the corners of a button round?

前端 未结 13 789
暖寄归人
暖寄归人 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:31

    style button with icon

       <Button
            android:id="@+id/buttonVisaProgress"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="5dp"
            android:background="@drawable/shape"
            android:onClick="visaProgress"
            android:drawableTop="@drawable/ic_1468863158_double_loop"
            android:padding="10dp"
            android:text="Visa Progress"
            android:textColor="@android:color/white" />
    

    shape.xml

        <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="14dp" />
    <gradient
        android:angle="45"
        android:centerColor="#1FA8D1"
        android:centerX="35%"
        android:endColor="#060d96"
        android:startColor="#0e7e1d"
        android:type="linear" />
    <padding
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp" />
    <size
        android:width="270dp"
        android:height="60dp" />
    <stroke
        android:width="3dp"
        android:color="#000000" />
    

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

    If you want change corner radius as well as want a ripple effect in button when pressed use this:-

    1. Put button_background.xml in drawable
    <?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="#F7941D">
    
        <item android:id="@android:id/mask">
            <shape android:shape="rectangle">
                <solid android:color="#F7941D" />
                <corners android:radius="10dp" />
            </shape>
        </item>
    
        <item android:id="@android:id/background">
            <shape android:shape="rectangle">
                <solid android:color="#FFFFFF" />
                <corners android:radius="10dp" />
            </shape>
        </item>
    
    </ripple>
    
    1. Apply this background to your button
    <Button
        android:background="@drawable/button_background"
        android:id="@+id/myBtn"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="My Button" />
    
    0 讨论(0)
  • 2020-11-22 17:37

    If you want something like this

    Button preview

    here is the code.

    1.Create a xml file in your drawable folder like mybutton.xml and paste the following markup:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:state_pressed="true" >
            <shape android:shape="rectangle"  >
                <corners android:radius="3dip" />
                <stroke android:width="1dip" android:color="#5e7974" />
                <gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92"  />            
            </shape>
        </item>
        <item android:state_focused="true">
            <shape android:shape="rectangle"  >
                <corners android:radius="3dip" />
                <stroke android:width="1dip" android:color="#5e7974" />
                <solid android:color="#58857e"/>       
            </shape>
        </item>  
        <item >
           <shape android:shape="rectangle"  >
                <corners android:radius="3dip" />
                <stroke android:width="1dip" android:color="#5e7974" />
                <gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />            
           </shape>
        </item>
    </selector>
    

    2.Now use this drawable for the background of your view. If the view is button then something like this:

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="#ffffff"
        android:background="@drawable/mybutton"
        android:text="Buttons" />
    
    0 讨论(0)
  • 2020-11-22 17:38

    Simple way i found out was to make a new xml file in the drawable folder and then point the buttons background to that xml file. heres the code i used:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    
    <solid android:color="#ff8100"/>
    <corners android:radius="5dp"/>
    
    </shape>
    
    0 讨论(0)
  • 2020-11-22 17:39

    Is there an easy way to achieve this in Android?

    Yes, today there is, and it is very simple.
    Just use the MaterialButton in the Material Components library with the app:cornerRadius attribute.

    Something like:

        <com.google.android.material.button.MaterialButton
            android:text="BUTTON"
            app:cornerRadius="8dp"
            ../>
    

    It is enough to obtain a Button with rounded corners.

    You can use one of Material button styles. For example:

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        .../>
    

    Also starting from the version 1.1.0 you can also change the shape of your button. Just use the shapeAppearanceOverlay attribute in the button style:

      <style name="MyButtonStyle" parent="Widget.MaterialComponents.Button">
        <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Button.Rounded</item>
      </style>
    
      <style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">16dp</item>
      </style>
    

    Then just use:

    <com.google.android.material.button.MaterialButton
       style="@style/MyButtonStyle"
       .../>
    

    You can also apply the shapeAppearanceOverlay in the xml layout:

    <com.google.android.material.button.MaterialButton
       app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.Button.Rounded"
       .../>
    

    The shapeAppearance allows also to have different shape and dimension for each corner:

    <style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
        <item name="cornerFamily">rounded</item>
        <item name="cornerFamilyTopRight">cut</item>
        <item name="cornerFamilyBottomRight">cut</item>
        <item name="cornerSizeTopLeft">32dp</item>
        <item name="cornerSizeBottomLeft">32dp</item>
    </style>
    

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

    Create rounded_btn.xml file in Drawable folder...

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
         <solid android:color="@color/#FFFFFF"/>    
    
         <stroke android:width="1dp"
            android:color="@color/#000000"
            />
    
         <padding android:left="1dp"
             android:top="1dp"
             android:right="1dp"
             android:bottom="1dp"
             /> 
    
         <corners android:bottomRightRadius="5dip" android:bottomLeftRadius="5dip" 
             android:topLeftRadius="5dip" android:topRightRadius="5dip"/> 
      </shape>
    

    and use this.xml file as a button background

    <Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/rounded_btn"
    android:text="Test" />
    
    0 讨论(0)
提交回复
热议问题