我有这个按钮XML,它具有透明背景,当点击时,颜色会发生变化,因此它会显示"点击效果"。
Drawable XML:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- press-->
<item android:drawable="@android:color/transparent" android:state_pressed="false" />
<!-- focused-->
<item style="@style/AppTheme" android:drawable="@color/color_primary" android:state_pressed="true" />
<!-- normal-->
<item android:drawable="@android:color/transparent" />
</selector>
我的问题是,如何在此按钮上添加边框?或者是一个XML示例,因此您可以获得相同的结果。谢谢!
答案 0 :(得分:1)
创建一个drawable,类似于这个,selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ff0000"/>
<stroke
android:width="1dp"
android:color="#00ff00" />
</shape>
然后改变这一行
<item
style="@style/AppTheme"
android:drawable="@color/color_primary"
android:state_pressed="true" />
到
<item
style="@style/AppTheme"
android:drawable="@drawable/selected"
android:state_pressed="true" />
solid是背景颜色,border是行程,你可以创建在选择器的不同状态下使用的drawable
答案 1 :(得分:0)
您可以这样做: 1.首先创建一个这样的可绘制文件:
[-0.157853, -0.156875, -0.143714, -0.140489, 0.058926, 0.128758, 0.128814, 0.14242, 0.140013]
[-0.208137, -0.206691, -0.266791, -0.262729, -0.18473, 0.023341, 0.023405, 0.042728, 0.039605]
[-0.086045, -0.085256, 0.022683, 0.025286, 0.246328, 0.215005, 0.215043, 0.224379, 0.222578]
然后在您提供的代码中替换以下行:
<shape android:shape="rectangle">
<!--apply button background transparent, full opacity-->
<solid android:color="#00ffffff"/>
<!--make button border solid color, nontransparent-->
<stroke android:color="#stroke color" android:width="2dp"/>
<corners android:radius="2dp"/>
</shape>
作为
<!-- press-->
<item android:drawable="path to above drawable" android:state_pressed="false" />
<!-- focused-->
<item style="@style/AppTheme" android:drawable="@color/color_primary" android:state_pressed="true" />
<!-- normal-->
<item android:drawable="path to above drawable" />