android – 我应该使用ImageButton或Button?

我有一个按钮有两个状态(选择和未选择).按钮的图像对于状态是不同的.我应该使用哪一个?我如何设置图像和状态?请提出建议(我是 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.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...