how to make a capsule shape Button in android?

后端 未结 7 2003
一整个雨季
一整个雨季 2021-01-31 15:56

I want to make a button exactly like in this image

\"Capsule

I want to use a xml file that will us

相关标签:
7条回答
  • 2021-01-31 16:18

    Try below, works with any view size:

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <corners android:radius="1000dp" />
    
        <solid android:color="@android:color/black" />
    
    </shape>
    
    0 讨论(0)
  • 2021-01-31 16:21

    Here is code to create button in xml,but if you want to create button as a capsule shaped you have to add the background

            <Button
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                **android:background="@drawable/button_background"**
                android:text="@string/image" >
            </Button>
    

    create button_background.xml in drawable folder,write the following code in button_background.xml

             <?xml version="1.0" encoding="UTF-8"?>
    
              <shape xmlns:android="http://schemas.android.com/apk/res/android"
    
                 android:shape="rectangle" android:padding="40dp">
    
              <!-- you can use any color you want I used here gray color-->
    
             <solid android:color="#01A9DB"/>
    
              <corners
    
              android:bottomRightRadius="20dp"
    
              android:bottomLeftRadius="20dp"
    
              android:topLeftRadius="20dp"
    
              android:topRightRadius="20dp"/>
    
          </shape>
    
    0 讨论(0)
  • 2021-01-31 16:23

    consider customizing a shape to it and use corners inside that shape:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <corners android:radius="10dp"/> <!-- increasing the value, increases the rounding. And as  TTransmit said, to make it like a capsule make the radius half of your button height -->
        <solid android:color="#AAAAAA"/> <!-- the button color -->
    
    </shape>
    

    So, save that shape in your /drawable folder, let's say it will be saved as "button_bg.xml", so when declaring the Button in your layout xml:

    <Button
        android:background="@drawable/button_bg"
        android:layout_height="20dp"
                    .
                    .                          />
    
    0 讨论(0)
  • 2021-01-31 16:25

    finally I found the way to do it with xml file. here is the code of the xml file that gave me the capsule shape button.

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    
      <corners
        android:bottomLeftRadius="30dp"
        android:bottomRightRadius="30dp"
        android:radius="60dp"
        android:topLeftRadius="30dp"
        android:topRightRadius="30dp" />
    
      <solid android:color="#CFCFCF" />
    
      <padding
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp" />
    
      <size
        android:height="60dp"
        android:width="270dp" />
    
    </shape>    
    
    0 讨论(0)
  • 2021-01-31 16:27

    It's called a Chip in Material and can be used like so:

    <com.google.android.material.chip.Chip
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>
    

    More info can be found here and here.

    To use Material components in your project, you'll to add the appropriate lib to your build.gradle:

    dependencies {
        // ...
        implementation 'com.google.android.material:material:1.0.0-beta01'
        // ...
      }
    

    More info on adding Material to your project can be found here.

    Alternatively, you can use the latest version of the support design lib:

    <android.support.design.chip.Chip
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:chipText="@string/hello_world"/>
    

    Also pull in the appropriate libs:

    dependencies {
        // ...
        implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
        implementation 'com.android.support:design:28.0.0-alpha1'
        // OR
        implementation 'com.android.support:design-chip:28.0.0-alpha1'
        // ...
    }
    

    See this answer for more on the latter approach.

    0 讨论(0)
  • 2021-01-31 16:28

    Just use a MaterialButton using the app:cornerRadius attribute and your favorite width.

     <com.google.android.material.button.MaterialButton
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                app:cornerRadius="18dp"
                android:text="BUTTON"
                />
    

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