android - 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 绝对是灰色,而不是紫色。感谢所有答案
解决方案
这是我的绿色圆形按钮的代码,您可以根据自己的方便进行修改。希望这会奏效。
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>
推荐阅读
- git - 如何从分支中删除提交只是为了以后干净地添加它们(没有冲突)?
- java - 是否可以在没有 revinfo 外键的情况下手动创建 Envers 审计表?
- swagger - Swagger 示例值未显示所有属性
- html - 如何让图像在表格单元格内垂直缩放?
- material-design - 为什么材料设计的输入大小不受封闭div的限制?
- python - While 循环不断提示用户提供正确的输入类型
- r - 从 R 中每个列表的数据框中的特定列中减去一个值
- r - 使用 tidyverse 创建变量而不是基础 R
- python-3.x - Python 3.x:Pandas Dataframe 我们如何更改特定范围的列名?
- node.js - 如何使用 env 参数测试 nodejs 的 index.js?