如何在android中设置按钮选择颜色和圆角?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12427107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set button selection color along with rounded corners in android?
提问by Bharath
I want to set a rounded corner for a button in android along with changing the button color on when selected. I am doing the following things.
我想为 android 中的按钮设置一个圆角,并在选择时更改按钮颜色。我正在做以下事情。
drawable/push_button.xml
drawable/push_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@color/green"/>
<item android:state_focused="true" android:drawable="@color/green"/>
<item android:state_focused="false" android:drawable="@color/green"/>
<item android:state_pressed="false" android:drawable="@color/red"/>
<item android:drawable="@drawable/push_button_background"/>
</selector>
drawable/push_button_background.xml
drawable/push_button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<solid android:color="#3F4040"/>
<corners
android:radius="7dp"
/>
</shape>
and in code, I am using
在代码中,我正在使用
android:background="@drawable/push_button"
Here, the problem is, button colors are setting properly when selected & deselected. But, rounded corners are not working.
在这里,问题是,按钮颜色在选择并取消选择时正确设置。但是,圆角不起作用。
How to do that? If I use
怎么做?如果我使用
android:background="@drawable/push_button_background"
then, rounded corners are working but the button color change on selection is not working
然后,圆角正在工作,但选择时的按钮颜色更改不起作用
How to implement this?
如何实施?
I have referred thislink. Even then no help!!
我已经提到了这个链接。即使那样也没有帮助!!
回答by Bharath
I have found answer for my question with few trial & error attempts.
我通过几次尝试和错误尝试找到了我的问题的答案。
Here is the solution.
这是解决方案。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true">
<shape >
<solid android:color="@color/green"/>
<corners
android:radius="7dp"/>
</shape>
</item>
<item android:state_focused="true" >
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/green"/>
<corners
android:radius="7dp"/>
</shape>
</item>
<item android:state_focused="false" >
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/red"/>
<corners
android:radius="7dp"/>
</shape>
</item>
<item android:state_pressed="false" >
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/red"/>
<corners
android:radius="7dp"
/>
</shape>
</item>
</selector>
回答by zabawaba99
What I did was defined the shape and specify the dp of each corner.
我所做的是定义形状并指定每个角的 dp。
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="90dp">
<solid android:color="#FFFFFF"/>
<padding />
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
If you increase the dp in each corner it will make the button more rounded.
如果您增加每个角的 dp,它将使按钮更圆润。
回答by Rich
I have been successful at doing exactly what you're describing. I did the following:
我已经成功地完成了你所描述的事情。我做了以下事情:
First, I created a new class that extends Button. In this class, I created a method called setState():
首先,我创建了一个扩展 Button 的新类。在这个类中,我创建了一个名为 setState() 的方法:
public void setState (int s)
{
if (s > 0 && s < 4 &&)
{
this.state = s;
switch (state)
{
case 1:
setBackgroundDrawable (def_gray);
break;
case 2:
setBackgroundDrawable (lt_gray);
break;
case 3:
setBackgroundDrawable (black);
}
}
}
The three background drawables you see above are XML files describing the button look. They are mostly the same, but vary in the color set for each one. The default gray button is described like this:
您在上面看到的三个背景可绘制对象是描述按钮外观的 XML 文件。它们大多相同,但每个的颜色设置不同。默认的灰色按钮是这样描述的:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#333333"
android:endColor="#333333" />
<stroke
android:width="2dp"
android:color="@android:color/white" />
<corners
android:radius="5dp" />
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
</shape>
</item>
</selector>
As far as I know, that XML format is expecting to be used to configure a color gradient on the button face. Since that wasn't what I wanted, I set both color values the same and the background color is consistent. You may need to experiment a bit with color values, but it looks like you've already got a handle on that.
据我所知,该 XML 格式有望用于在按钮面上配置颜色渐变。由于这不是我想要的,我将两个颜色值设置为相同并且背景颜色一致。您可能需要对颜色值进行一些试验,但看起来您已经掌握了这一点。