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

為自訂 View 加上 XML 屬性:三個步驟完整說明

編輯:關於 Android 程式設計

前面已經寫過幾篇關於自訂 View 的文章,這裡就不再從頭談起,有興趣的讀者可以回頭看看之前的內容。先前的文章主要在說明「如何使用自訂 View」以及「為什麼要用自訂 View」,但實務上我們往往還有進一步的需求:希望能直接在 XML 裡就設定這個 View 的屬性,如下圖那樣寫。

要達成這件事,主要分成三個步驟:

  1. 自訂屬性名稱
  2. 把屬性名稱與控制項關聯起來
  3. 在版面檔中透過自訂命名空間取得這些屬性

大致流程可以參考下圖:

步驟一:自訂屬性名稱

首先在 values 目錄下新增一個 XML 檔(例如 atts.xml),在其中宣告你需要的屬性名稱與型別。這裡用 declare-styleable 標籤包起來,name 通常就取決aining自訂 View 的名稱。

 <?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyTitle">
        <!-- 左側文字 -->
        <attr name="leftText" format="string" />
        <!-- 中間標題 -->
        <attr name="titleText" format="string" />
        <!-- 右側文字 -->
        <attr name="rightText" format="string" />
        <!-- 文字顏色 -->
        <attr name="textColor" format="color" />
    </declare-styleable>
</resources> 

步驟二:把屬性名稱與控制項關聯

這一步做起來很直觀,關鍵是透過 obtainStyledAttributes() 把 XML 上寫的值讀出來,再手動套用到內部的子 View 上。另外別忘了在構造方法中加入 AttributeSet 參數,否則系統不會把 XML 屬性傳進來。

MyView.java

 package com.example.double2.viewxmltest;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 項目名稱:ViewXmlTest
 * 創建人:Double2號
 * 創建時間:2016/8/4 10:23
 */
public class MyView extends LinearLayout {

    private int colorText;
    private String textLeft;
    private String textTitle;
    private String textRight;

    private TextView tvLeft;
    private TextView tvTitle;
    private TextView tvRight;

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        // 從 XML 屬性中取出顏色與三個 string
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyTitle);
        colorText = ta.getColor(R.styleable.MyTitle_textColor, Color.BLACK);
        textLeft = ta.getString(R.styleable.MyTitle_leftText);
        textTitle = ta.getString(R.styleable.MyTitle_titleText);
        textRight = ta.getString(R.styleable.MyTitle_rightText);
        ta.recycle();

        // 載入內部版面,效果等同 setContentView()
        LayoutInflater.from(context).inflate(R.layout.my_view, this);
        tvLeft = (TextView) findViewById(R.id.tv_left);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvRight = (TextView) findViewById(R.id.tv_right);

        // 把 XML 上設定的值套用到子控制項
        tvLeft.setTextColor(colorText);
        tvLeft.setText(textLeft);
        tvTitle.setTextColor(colorText);
        tvTitle.setText(textTitle);
        tvRight.setTextColor(colorText);
        tvRight.setText(textRight);
    }
} 

my_view.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp"
    tools:background="@android:color/holo_blue_dark">

    <TextView
        android:id="@+id/tv_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        tools:text="left"
        tools:textColor="#fff" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:textSize="23sp"
        tools:text="title"
        tools:textColor="#fff" />

    <TextView
        android:id="@+id/tv_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        tools:text="right"
        tools:textColor="#fff" />
</LinearLayout> 

步驟三:透過命名空間取得自訂屬性

最後在使用這個 View 的版面檔中,必須先宣告自訂的命名空間。Android Studio 只需要寫 res-auto 即可;若使用 Eclipse,則要補上完整的套件名稱。

要注意的是:my_view 只是這個命名空間的「別名」,真正起作用的是後面的 https://schemas.android.com/apk/res-auto。

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:my_view="https://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.double2.viewxmltest.MyView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_dark"
        my_view:leftText="Back"
        my_view:rightText="Go"
        my_view:textColor="#fff"
        my_view:titleText="MyViewTest" />
</RelativeLayout> 

這樣一來,這個自訂 View 就能像系統原生控制項一樣,直接在 XML 裡設定屬性了。整體做完會發現,三個步驟其實環環相扣:先在 declare-styleable 定義屬性,再在 Java 端用 TypedArray 讀取並套用,最後在使用處宣告命名空間。少了任何一環,屬性都不會生效。

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