我有一个按钮有两个状态(选择和未选择).按钮的图像对于状态是不同的.我应该使用哪一个?我如何设置图像和状态?请提出建议(我是
Android的新手).
解决方法
在drawable文件夹中使用xml配置.您可以引用此xml配置(文件名),而不是将图像引用为按钮的背景:
例如:my_button.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/button_style1_active" /> <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/button_style1_down" /> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/button_style1_down" /> <item android:drawable="@drawable/button_style1_up" /> </selector>
在layout.xml中使用:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tap me" android:background="@drawable/my_button"/>
使用此配置,您可以影响按钮的外观,按下,聚焦等.它们对于两种类型的按钮(Button和ImageButton)都是一样的.如果您的按钮不包含文本,请使用ImageButton.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。