想要实现一个动态的点击效果,用于增强用户的体验。
第一步:在布局文件中,先写好控件的格式内容。
<Button
android:id="@+id/btn"
style="@style/btn_next_view_style"
android:text="下一步"
/>
第二步:在values中的styles.xml文件中,添加样式,在自定义格式中。
<!-- 按钮的样式 --> ///背景图片的显示,button_selector不是一张图片,是一个xml文件。
<style name="btn_next_view_style">
<item name="android:background">@drawable/button_selector</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@drawable/button_textcolor_selector</item>
</style>
第三步:在res文件夹中,创建一个drawable文件夹中,创建一个button_selector.xml和button_textcolor_selector.xml文件。(****文件中用到的背景图片,需要存放到drawable文件夹中****)
button_selector.xml,设置按钮按下和不按时的不同的背景图片。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/function_greenbutton_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/function_greenbutton_pressed" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@drawable/function_greenbutton_normal"/>
</selector>
button_textcolor_selector.xml,设置按钮上文字按下和不按时的颜色变化。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@drawable/red"/>
<item android:state_enabled="true" android:state_focused="true" android:color="@drawable/red"/>
<item android:color="@drawable/green"/>
</selector>
第四步:在资源文件中,设置颜色的值。在values文件夹下的创建一个color.xml文件。
<resources>
<!-- 文字的颜色变化 -->
<drawable name="red">#f00</drawable>
<drawable name="green">#0f0</drawable>
</resources>