Android控件之ImageButton


Android控件之ImageButton

1 ImageButton介紹

ImageButton是圖片按鈕,用戶能自定義按鈕的圖片。

ImageButton的drawable state值說明:
(01) android:drawable
    默認圖片,等於一個drawable資源
(02) android:state_pressed
    按下狀態的圖片
(03) android:state_focused
    獲得焦點狀態的圖片,比如用戶選擇了一ImageButton
(04) android:state_hovered
    光標懸停狀態的圖片,通常與focused state相同,它是4.0的新特性
(05) android:state_selected
    選中狀態的圖片,它與focus state並不完全一樣,如一個list view 被選中的時候,它里面的各個子組件可能通過方向鍵,被選中了。
(06) android:state_checkable
    按鈕能否check,值為true或false。如果這個項目要用於對象的可選擇狀態,那么就要設置為true。如果這個項目要用於不可選狀態,那么就要設置為false。(它只用於一個對象在可選和不可選之間的轉換)。
(07) android:state_checked
    選中狀態的圖片
(08) android:state_enabled
    使用狀態(比如,按鈕能被正常點擊狀態)的圖片,能夠接受觸摸或者點擊事件
(09) android:state_activated
    按鈕被激活狀態的圖片
(10) android:state_window_focused
    如果這個項目要用於應用程序窗口的有焦點狀態(應用程序是在前台),那么就要設置為true,否者設置false。


2 ImageButton示例

創建一個activity,包含一個ImageButton:按下和未按下狀態,分別顯示不同圖片。

應用層代碼

 

package com.skywang.control;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class ImageButtonTest extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_button_test);
    }
   
}

layout文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@drawable/bt_del" />

</LinearLayout>

bt_del.xml內容如下:

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <!-- 按下狀態 -->
     <item android:state_pressed="true"
           android:drawable="@drawable/bt_del_down" />
     <!-- 獲取焦點狀態 -->
     <item android:state_focused="true"
           android:drawable="@drawable/bt_del_up" />
     <!-- 默認狀態 -->
     <item android:drawable="@drawable/bt_del_up" />
 </selector>

bt_del_up.png如下:

bt_del_down如下: 

manifest文件

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.skywang.control"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.skywang.control.ImageButtonTest"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

點擊下載源代碼

 

運行效果圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM