how to make a capsule shape Button in android?

后端 未结 7 2004
一整个雨季
一整个雨季 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:39

    I haven't tried all of the answers here, but I believe some/all of them are works. The accepted answer is also works, but it can be simplified. Since I love elegant and simple solution, I come up with my own solution. Basically, we just need to add radius big enough compared with your View width and height, so it will create a round corner. In this case I put 1000dp to make it safe. We don't even need to add android:shape="rectangle" at all.

    Just follow these 2 simple steps:

    1. Create an XML file in your drawable folder. For example let's name it bg.xml

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android">
          <corners android:radius="1000dp"/>
      
          <solid android:color="@color/yourPreferredColor"/>
      </shape>
      
    2. Then you can use it in layout XML file as your View property android:background="@drawable/bg" or directly in the code view.setBackgroundResource(R.drawable.bg)

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