How I can do a button with left/right side as roun

2019-08-03 09:54发布

问题:

I'm trying to do a button with the sides rounded, but when I change the size of screen the button sides left to be rounded as circles, doesn't mantain the aspect ratio (I think).

I want to have the next togglebutton in everysize. How I can do an XML with the sides rounded for every resolution?

I've read this, but is not a solution (How to make the corners of a button round?)

回答1:

The other answers are correct... almost: if you will set corners to some value (let's say 20dp), but your button height will be greater (like 40dp) - you will end with rounded corners instead of rounded sides. The solution is thus straightforward: set android:corners attribute to some high enough value that will be greater than button height:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorPrimary" />
    <corners android:radius="200dp" />
    <size
        android:height="16dp"
        android:width="32dp" />
</shape>



回答2:

Create a drawable resource file in xml. The following is code for a blue button

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#2196f3"></solid>

    <corners android:radius="12dp"></corners>
</shape>

Then just asign this resource file as the background to one of your textViews

I hope this helps



回答3:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <corners android:radius="20dp" />
    <solid android:color="#1ee" />
</shape>