问题描述
API 22。我想为我的按钮设置一个边框半径,所以我这样做了:How to make the corners of a button round? (我尝试了正确的答案)。 How to change the color of a button? 这里的方法也都不起作用。 我曾经使用 android:backgroundTint 设置按钮的背景颜色。那是唯一有效的方法。不幸的是,当我通过 android:background="@drawable/roundbutton" 将文件链接到我的按钮时,我通过 android:backgroundTint 选择的颜色将不起作用。以及我在 drawable/roundbutton 中定义的纯色。奇怪的是,边界半径有效: 我在 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 绝对是灰色,而不是紫色。 谢谢大家的回答
解决方法
这是我的绿色圆形按钮代码,您可以根据自己的方便进行修改。 希望这会奏效。
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>
,
通过在 themes.xml 中创建新样式并将其设置为按钮的主题,我能够解决我的问题。这是我在 themes.xml 中的代码:
<style name="Theme.ButtonWhite" parent="Base.Widget.AppCompat.Button.Colored">
<item name="colorPrimary">@color/white</item>
</style>
activity_main.xml 中按钮的代码:
android:theme="@style/Theme.ButtonWhite"
我真的在问自己为什么其他人似乎没有这个问题,因为我在创建一个全新的项目时遇到了完全相同的问题