我有一个具有两种状态(选中和未选中)的按钮。按钮的图像因状态而异。我应该使用哪一个?如何设置图像和状态?请提出建议(我是android新手)。
问问题
4867 次
1 回答
14
在可绘制文件夹中使用 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。
于 2010-11-24T08:49:18.343 回答