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

Android 自定義控制項與自定義屬性完整教學

編輯:關於 Android 程式設計

自定義控制項在 Android 中幾乎無所不在。當一個畫面需要把 ImageView、ImageButton、TextView 等控件組合在一起,而且這種組合會被重複使用時,與其每次重寫,不如封裝成一個自訂 View。這樣不僅提升開發效率,在 XML 佈局中也顯得整潔美觀。

一、自定義屬性的型別

以下屬性都定義在 values/attrs.xml 中,名稱可自由命名:

  • reference:參考某一資源 ID
  • color:顏色值
  • boolean:布林值
  • dimension:尺寸值
  • float:浮點值
  • integer:整數值
  • string:字串
  • fraction:百分比
  • enum:列舉值
  • flag:位元或運算
  • 多型別:以 | 串接,如 reference|color

例如定義一個包含多種屬性的 styleable:

<declare-styleable name = "custom_view">
    <attr name = "custom_id" format = "integer" />
    <attr name = "src" format = "reference" />
    <attr name = "background" format = "reference" />
    <attr name = "text" format = "string" />
    <attr name = "textColor" format = "color" />
    <attr name = "textSize" format = "dimension" />
</declare-styleable>

二、實現自定義控制項

以一個「帶陰影背景、下方有文字說明的按鈕」為例,讓 CustomView 繼承 FrameLayout,在構造函數中透過 TypedArray 讀取我們自訂的屬性:

public class CustomView extends FrameLayout implements OnClickListener {

    private CustomListener customListener = null;
    private Drawable mSrc = null, mBackground = null;
    private String mText = "";
    private int mTextColor = 0;
    private float mTextSize = 20;
    private int mCustomId = 0;

    private ImageButton mButtonView = null;
    private TextView mTextView = null;
    private ImageView mBackgroundView = null;

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.custom_view);

        mSrc = a.getDrawable(R.styleable.custom_view_src);
        mBackground = a.getDrawable(R.styleable.custom_view_background);
        mText = a.getString(R.styleable.custom_view_text);
        mTextColor = a.getColor(R.styleable.custom_view_textColor, Color.WHITE);
        mTextSize = a.getDimension(R.styleable.custom_view_textSize, 20);
        mCustomId = a.getInt(R.styleable.custom_view_custom_id, 0);

        // 建立背景、按鈕、文字三個子 View 並 addView
        // ...
        a.recycle();
    }

    public void setCustomListener(CustomListener l) {
        customListener = l;
    }

    @Override
    public void onClick(View v) {
        if (customListener != null) {
            customListener.onCuscomClick(v, mCustomId);
        }
    }

    public interface CustomListener {
        void onCuscomClick(View v, int custom_id);
    }
}

三、在佈局中使用

使用時記得先在根佈局宣告命名空間前綴 xmlns:nanlus="https://schemas.android.com/apk/res/你的包名",接著就能像原生控件一樣設定自訂屬性:

<com.nanlus.custom.CustomView
    android:id="@+id/custom1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    nanlus:background="@drawable/background"
    nanlus:custom_id="1"
    nanlus:src="@drawable/style_button"
    nanlus:text="按鈕1" />

四、在 Activity 中綁定事件

public class CustomActivity extends BaseActivity implements CustomListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ((CustomView) this.findViewById(R.id.custom1)).setCustomListener(this);
    }

    @Override
    public void onCuscomClick(View v, int custom_id) {
        switch (custom_id) {
            case 1:
                Toast.makeText(this, "hello !!!", Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }
}

透過這樣的封裝,一個複合控制項就能在 XML 中以宣告式的方式靈活配置,大幅減少重複的樣板程式碼。

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