首页 > 解决方案 > Android:按钮上的纯色不起作用

问题描述

API 22. 我想为我的按钮设置一个边框半径,所以我这样做了:如何使按钮的角变圆? (我尝试了正确的答案)。如何更改按钮的颜色? 这里的任何方法都不起作用。我曾经使用 android:backgroundTint 设置按钮的背景颜色。那是唯一有效的方法。不幸的是,当我通过 android:background="@drawable/roundbutton" 将文件链接到我的按钮时,我通过 android:backgroundTint 选择的颜色不起作用。以及我在可绘制/圆形按钮中定义的纯色。奇怪的是,边界半径有效:我在 activity_main.xml 中的代码:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/signinotherbutton"
    android:background="@drawable/roundbutton"
    android:backgroundTint="@color/grey"
    android:textColor="@color/white"
    android:textAllCaps="false"/>

我的 drawable/roundbutton.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <solid android:color="@color/grey"/>
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <solid android:color="@color/grey"/>
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <solid android:color="@color/grey"/>
        </shape>
    </item>
</selector>

结果:具有我定义的半径的按钮,但具有默认颜色(大约紫色)不,@color/grey 绝对是灰色,而不是紫色。感谢所有答案

标签: androidbuttoncolorsradius

解决方案


这是我的绿色圆形按钮的代码,您可以根据自己的方便进行修改。希望这会奏效。

button_rounded.xml

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

推荐阅读