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


要達成這件事,主要分成三個步驟:
大致流程可以參考下圖:

首先在 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 讀取並套用,最後在使用處宣告命名空間。少了任何一環,屬性都不會生效。
TabLayout 自定義滑塊背景:SliderLayout 完整解析
TabLayout 是 Material Design 中的一個控件,能和 ViewPager 搭配出聯動效果。但默認的下劃線指示器並不能滿足所有設計稿——有時候我們需要
Android-Universal-Image-Loader 源碼解讀:圖片異步加載與緩存
前言: 在 Android 開發中,圖片加載是個老生常談卻又頗具挑戰的問題。處理不好,各種怪問題層出不窮,比如加載導致介面卡頓、程式 crash。因此,如何高效加載大量圖
Android VR 實戰:用 Google VR SDK 播放 360 度全景影片
VR(Virtual Reality,虛擬現實)是一套以電腦模擬出的三維環境,讓使用者透過頭戴裝置沉浸在畫面之中。要在 Android 上做出 360 度全景影片播放,最
動手玩 Android 屬性動畫(下):Interpolator 與 ViewPropertyAnimator
Interpolator 的用法 Interpolator 這個名詞不太好直譯,字面意思是「補間器」,它的主要作用是控制動畫的變化速率,用來實現某種非線性的運動效果。所謂