我应该使用ImageButton或Button?

xyd*_*dev 10 android button imagebutton

我有一个按钮有两个状态(选中和未选中).按钮的图像对于状态是不同的.我应该使用哪一个?我如何设置图像和状态?请给出建议(我是android的新手).

Imp*_*ion 14

在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>
Run Code Online (Sandbox Code Playgroud)

在layout.xml中使用:

<Button android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Tap me" 
        android:background="@drawable/my_button"/>
Run Code Online (Sandbox Code Playgroud)

使用此配置,您可以在按下,聚焦等时影响按钮的外观.它对于两种类型的按钮(Button和ImageButton)都是一样的.如果您的按钮不包含任何文本,请使用ImageButton.