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

追查 Android 控制項的預設屬性從哪裡來

編輯:關於 Android 程式設計

寫 XML 版面時我們很少替 TextView 指定 style,但它就是有字級、有顏色、有邊距。這些預設值到底從哪冒出來的?答案藏在 Theme 與 Style 的三層關係裡。

一、先看 TextView 的三個建構子

public TextView(Context context) {
    this(context, null);
}

public TextView(Context context, AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.textViewStyle);
}

@SuppressWarnings("deprecation")
public TextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // ...
    TypedArray a = context.obtainStyledAttributes(
            attrs, com.android.internal.R.styleable.TextView, defStyle, 0);
    // ...
}

Android 提供的控制項幾乎都有這三個建構子,用途各不相同:

  • 第一個(只有 Context):給你在程式碼中 new TextView(this) 使用。
  • 第二個(Context + AttributeSet):XML 解析時,未指定 style 會呼叫這個。
  • 第三個(Context + AttributeSet + defStyle):XML 解析時,已指定 style 會呼叫這個。

二、預設 style 是怎麼被帶進去的

我們通常不指定 style,所以走第二個建構子。而它做的事就是把 com.android.internal.R.attr.textViewStyle 當成 defStyle 傳給第三個建構子:

public TextView(Context context, AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.textViewStyle);
}

這個 textViewStyle 只是一個 ID 參照,定義在系統的 attr.xml:

<!-- Default TextView style. -->
<attr name="textViewStyle" format="reference" />

問題來了:它只是一個「指標」,本身沒有值。那值在哪?

三、Theme 才是答案

預設 style 的值是在 Activity 的 Theme 中指定的:

<style name="Theme">
    <item name="textViewStyle">@android:style/Widget.TextView</item>
</style>

Theme 是所有主題的基類,所以無論你的 Activity 套用哪個 theme,都能沿著繼承鏈找到 textViewStyle。這也解釋了一個常見現象:換 theme 會讓控制項外觀全變,因為所有預設 style 指標都指向不同目標。

而 @android:style/Widget.TextView 指向系統的 style 資源:

<style name="Widget.TextView">
    <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
    <item name="android:textSelectHandleLeft">?android:attr/textSelectHandleLeft</item>
    <item name="android:textSelectHandleRight">?android:attr/textSelectHandleRight</item>
    <item name="android:textSelectHandle">?android:attr/textSelectHandle</item>
</style>

注意 ?android:attr/ 和 @android:style/ 的差別:? 表示「從目前主題中取值」,@ 表示「直接參照這個資源」。所以 textAppearanceSmall 又會依主題不同而變化,形成一層層的間接參照。

四、obtainStyledAttributes 在做什麼

TypedArray a = context.obtainStyledAttributes(
        attrs, com.android.internal.R.styleable.TextView, defStyle, 0);

這行程式可以理解為:用 attrs 和 defStyle 兩份來源,去解析 R.styleable.TextView 中宣告的那批屬性。三個來源概念的分工是:

  • attrs:XML 版面中你直接寫在標籤上的屬性值。
  • defStyle:style 中指定的屬性值。
  • R.styleable.TextView:在 attrs.xml 中定義的屬性集合(相当于一個 schema)。

解析的優先順序是「XML 直接寫的 > style > 主題預設」,這就是為什麼你在 XML 寫 android:textSize="30sp" 能蓋掉主題的字級。

最後再用 TypedArray 的 getter 把值取出來:

mThumbDrawable = a.getDrawable(R.styleable.Switch_thumb);
mTrackDrawable = a.getDrawable(R.styleable.Switch_track);
mTextOn = a.getText(R.styleable.Switch_textOn);
mTextOff = a.getText(R.styleable.Switch_textOff);

五、總結這條鏈

  1. 控制項的預設值寫在 style 中。
  2. 不指定 style 時,會使用由 defStyle 參數指定的預設 style。
  3. 而預設 style 的實際值,是在 Theme 中透過 textViewStyle 這類 item 指定的。
  4. XML 直接寫的屬性優先級最高,可以覆蓋以上全部。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved