如何给按钮加上边框和点击效果?
How to put border on button and click effect?
我有这个按钮 XML,它具有透明背景,单击时颜色会改变,因此显示 "click effect"。
可绘制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 例子,所以你有相同的结果。谢谢!
创建一个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" />
实心是背景色,边框是描边,你可以创建在选择器的不同状态下使用的drawable
你可以这样做:
1.首先像这样创建一个drawable文件:
<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" />
作为
<!-- 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" />
我有这个按钮 XML,它具有透明背景,单击时颜色会改变,因此显示 "click effect"。
可绘制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 例子,所以你有相同的结果。谢谢!
创建一个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" />
实心是背景色,边框是描边,你可以创建在选择器的不同状态下使用的drawable
你可以这样做: 1.首先像这样创建一个drawable文件:
<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" />
作为
<!-- 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" />