如何在Android中设置带圆角的按钮选择颜色?

20
我想在Android中设置一个带有圆角的按钮,并在选中时改变按钮颜色。 我正在执行以下操作。
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>

可绘制/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>

在代码中,我正在使用

android:background="@drawable/push_button"

这里的问题是,当选择或取消选择按钮时,按钮颜色设置正确。但是,圆角无法生效。

如何解决? 如果我使用

android:background="@drawable/push_button_background"

圆角已经生效了,但选中后按钮的颜色变化没有生效。

如何实现它?

我已经参考了这个链接,但还是没有帮助!


您可能需要定义另外几个具有所需颜色和半径的“drawables”,并将“@color/color”更改为您定义的“drawables”。 - Aprian
@Aprian.. 你能详细解释一下吗?我是 Android 的新手!!你能给一些示例吗? - Bharath
3个回答

66

我通过几次尝试和错误的尝试找到了我的问题的答案。

这是解决方案。

<?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>

只是想指出,我发现这可能会根据您使用的父样式而无法正常工作。在我的情况下,使用 Widget.MaterialComponents.Button 无法正常工作,但是使用 Widget.MaterialComponents.Button.TextButton 可以正常工作。 - Alberto

2
我所做的是定义形状并指定每个角的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>

如果在每个角上增加dp,将使按钮更圆润。

你尝试过使用样式吗?你可以为一个按钮创建一个样式,然后将其应用于任何按钮。我以前没有尝试过对按钮使用它,但我知道你可以使用样式添加选择器并更改视图的不同方面(高度、颜色、选择器、图像等)。 - zabawaba99

0

我已经成功地完成了你所描述的任务。我采取了以下步骤:

首先,我创建了一个继承自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);
        }
    }
}

您在上面看到的三个背景可绘制对象是描述按钮外观的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>

据我所知,该XML格式期望用于配置按钮表面的渐变颜色。由于这不是我想要的,我将两个颜色值设置为相同,使背景颜色保持一致。你可能需要尝试一下颜色值,但看起来你已经掌握了这个技巧。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接