Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android UI 自定義控件入門——打造自己的 ImageButton

編輯:關於 Android 程式設計

概述

我們在使用一些 App 時,應該很少看到完全不加修飾的「裸控件」吧?除非是系統內建軟體,為了風格統一而做出權衡。這裡並不是要批評 Android 原生控件不好看——說實話,我很喜歡 Android 的一些原生控件,只是為了整體風格的一致性,偶爾還是得在美工上多花點心思。就這點而言,我對某訊的產品設計頗為欣賞。接下來,就讓我們一步步走進 Android UI 自定義控件的世界。

分析

自定義控件就像堆積木,我們挑選積木、為它上色、再賦予它功能說明。下面用一個小例子來逐一討論。

示例分析

效果預覽

本示例選用 ImageButton 做一個簡單分析,先看看運行效果:

搭建基本雛形

搭雛形時,第一步是挑選積木。我們選一個 ImageView 和一個 TextView,上下擺放,再用適當的間距把兩者綁在一起:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:paddingBottom="5dip"
        android:paddingTop="5dip"
        android:src="@drawable/right_icon" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:text="確定"
        android:textColor="#000000" />
</LinearLayout>

這段程式碼只能得到一個居中的方圖、下方的文字,以及貼合兩者邊緣的間距。

外觀設計與功能添加

接下來我們要為它分配顏色、賦予功能,這些都實現在 Java 程式碼中。關鍵程式碼如下:

/** 設置圖片資源 */
public void setImageResource(int resId) {
    imageView.setImageResource(resId);
}
/** 設置顯示的文字 */
public void setTextViewText(String text) {
    textView.setText(text);
}

不過僅僅讓這個「Button」更好看還不夠,我們還要做另一件事:例如點擊後要執行一些綁定的、使用該控件時必然會進行的操作。這與「加外套」是同一個道理:

@Override
public void setOnClickListener(OnClickListener l) {
    auxiliaryFunction();
    super.setOnClickListener(l);
}
protected void auxiliaryFunction() {
    Log.i(TAG, "log message.");
}

這個額外功能是否真的執行,可以在 Log 日誌中查看確認。

既然是自定義,這個 ImageButton 的原始父類別自然不是 Button。真相如下:

public class ImageButton extends LinearLayout {
    private ImageView imageView;
    private TextView textView;

    public ImageButton(Context context) {
        super(context);
    }

    public ImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.image_button, this);
        imageView = findViewById(R.id.imageView1);
        textView = findViewById(R.id. textView1);
    }

    public void setImageResource(int resId) {
        imageView.setImageResource(resId);
    }

    public void setTextViewText(String text) {
        textView.setText(text);
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        auxiliaryFunction();
        super.setOnClickListener(l);
    }

    protected void auxiliaryFunction() {
        Log.i(TAG, "log message.");
    }
}

使用分析

1. 在 XML 中使用

這裡有一點要特別注意:必須指明自定義控件的完整類別路徑,如下:

<com.demo.customview.imagebutton.widgets.ImageButton
    android:id="@+id/btn_right"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:background="@drawable/custom_button" />

2. 配置觸摸效果

Button 的動作不外乎觸摸、按下與抬起三種,這三種效果的配置需要在 res 下的 drawable 資料夾中建立(若無此資料夾請新建一個)selector:

<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_unpress_background"
        android:state_focused="true" android:state_pressed="false" />
    <item android:drawable="@drawable/button_pressed_background"
        android:state_pressed="true" />
    <item android:drawable="@drawable/button_unpress_background"
        android:state_focused="false" android:state_pressed="false" />
</selector>

3. 在 Java 中使用

在 Java 程式碼中的用法與 Button 幾乎無異:

public class MainActivity extends Activity {
    private ImageButton mImageBtn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageBtn1 = findViewById(R.id.btn_right);
        mImageBtn1.setTextViewText("確定");
        mImageBtn1.setImageResource(R.drawable.right_icon);
        mImageBtn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "點擊確定", 0).show();
            }
        });
    }
}

從挑積木、上色,到賦予功能與觸摸回饋,一個專屬的 ImageButton 就這樣完成了。掌握這套思路後,你也能打造各種符合自身設計風格的自定義控件。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved