https://blog.csdn.net/zhao123h/article/details/52210732
在開發android開發過程中,很多人都會遇到自定義view,一般都需要繼承自View類,而當你打開View類的源碼時,發現會有四個構造函數,那么這四個構造函數是如何使用的呢,怎么合理的利用四個構造函數呢,本文將進行一定探究,希望能夠拋磚引玉。
一 View類的四個構造函數
先從android源碼中把四個構造函數拉出來看看。。
1 第一個構造函數
/**
* Simple constructor to use when creating a view from code.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
*/
public View(Context context)
2 第二個構造函數
/**
* Constructor that is called when inflating a view from XML. This is called
* when a view is being constructed from an XML file, supplying attributes
* that were specified in the XML file. This version uses a default style of
* 0, so the only attribute values applied are those in the Context's Theme
* and the given AttributeSet.
*
* <p>
* The method onFinishInflate() will be called after all children have been
* added.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs The attributes of the XML tag that is inflating the view.
* @see #View(Context, AttributeSet, int)
*/
public View(Context context, @Nullable AttributeSet attrs)
3 第三個構造函數
/**
* Perform inflation from XML and apply a class-specific base style from a
* theme attribute. This constructor of View allows subclasses to use their
* own base style when they are inflating. For example, a Button class's
* constructor would call this version of the super class constructor and
* supply <code>R.attr.buttonStyle</code> for <var>defStyleAttr</var>; this
* allows the theme's button style to modify all of the base view attributes
* (in particular its background) as well as the Button class's attributes.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs The attributes of the XML tag that is inflating the view.
* @param defStyleAttr An attribute in the current theme that contains a
* reference to a style resource that supplies default values for
* the view. Can be 0 to not look for defaults.
* @see #View(Context, AttributeSet)
*/
public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
4 第4個構造函數
/**
* Perform inflation from XML and apply a class-specific base style from a
* theme attribute or style resource. This constructor of View allows
* subclasses to use their own base style when they are inflating.
* <p>
* When determining the final value of a particular attribute, there are
* four inputs that come into play:
* <ol>
* <li>Any attribute values in the given AttributeSet.
* <li>The style resource specified in the AttributeSet (named "style").
* <li>The default style specified by <var>defStyleAttr</var>.
* <li>The default style specified by <var>defStyleRes</var>.
* <li>The base values in this theme.
* </ol>
* <p>
* Each of these inputs is considered in-order, with the first listed taking
* precedence over the following ones. In other words, if in the
* AttributeSet you have supplied <code><Button * textColor="#ff000000"></code>
* , then the button's text will <em>always</em> be black, regardless of
* what is specified in any of the styles.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs The attributes of the XML tag that is inflating the view.
* @param defStyleAttr An attribute in the current theme that contains a
* reference to a style resource that supplies default values for
* the view. Can be 0 to not look for defaults.
* @param defStyleRes A resource identifier of a style resource that
* supplies default values for the view, used only if
* defStyleAttr is 0 or can not be found in the theme. Can be 0
* to not look for defaults.
* @see #View(Context, AttributeSet, int)
*/
public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)
這四個構造函數在源碼中都有注釋,為了便於理解,我將注釋也貼了出來,有點長,不過利於本文理解。
二 構造函數的調用
【第一個問題】
如果我們在繼承了View類實現自定義類時,需要重寫哪個構造函數?
回答這個問題,首先需要知道,在定義了View時,我們都調用了哪個構造函數。我這里做了一個簡單的實驗:
我們聲明一個簡單的View,繼承自TextView,什么都不改,只是在4個constructor中打印幾個tag,查看到底哪個構造函數被調用。
【實驗】
package com.zhaohui.code.view;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
import com.example.zhaohui.player.R;
/**
* Created by zhaohui on 16-8-12.
*/
public class CustomView extends TextView {
String tag = "customView";
public CustomView(Context context) {
super(context);
Log.d(tag,"First Constructor");
}
public CustomView(Context context, AttributeSet attrs) {
this(context,attrs,android.R.attr.textViewStyle);
Log.d(tag,"Second Constructor");
for(int i=0;i<attrs.getAttributeCount();i++){
Log.d(tag, attrs.getAttributeName(i)+" : "+attrs.getAttributeValue(i));
}
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr,0);
Log.d(tag,"Third Constructor");
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr,defStyleRes);
Log.d(tag,"Fourth Constructor");
}
}
在布局文件layout中加上這個View
<com.zhaohui.code.view.CustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CustomView"
android:textSize="30sp"/>
[實驗結果]
08-14 05:08:37.847 13687-13687/com.example.zhaohui.player D/customView: Second Constructor
08-14 05:08:37.847 13687-13687/com.example.zhaohui.player D/customView: textSize : 30.0sp
08-14 05:08:37.847 13687-13687/com.example.zhaohui.player D/customView: layout_width : -1
08-14 05:08:37.848 13687-13687/com.example.zhaohui.player D/customView: layout_height : -2
08-14 05:08:37.848 13687-13687/com.example.zhaohui.player D/customView: text : CustomView
【實驗—結果分析】
通過上面的實驗輸出中的Second Constructor,我們知道,當我們自定義一個View,且在布局文件中引用時,在系統初始化該View時,調用的是第二個構造函數,而且我還把第二個構造函數中的attrs參數也打了出來,可以看出其中的參數attrs是我們在xml中配置的參數。
其實第一個構造函數用途並不大,主要是在java代碼中聲明一個View時所用,不過如果只用第一個構造函數,聲明的View並沒有任何的參數,基本是個空的View對象。
三 View的第三和第四個構造函數
在回答了第一個問題后,還有后兩個構造函數,這是本文的重點。
1 View的屬性和主題
在說后兩個構造函數之前,先說說View的屬性,在View中有不同的屬性,比如layout_width等,TextView還有textColor這些特有的屬性,我們可以對這些屬性進行不同的配置進而實現不同的效果。而且屬性也可以在不同的位置進行配置。以TextView為例,android:textColor這個屬性可以在多個地方配置,可以直接寫在xml中,可以在xml中以style的形式定義,這兩種是我們平時見得較多的,其實還有一種背后的力量可以給屬性賦值,那就是主題。
我們在android中可以配置一個主題,從而使得一些View即使你不對其進行任何配置,它都會有一些已經默認賦值的屬性,這就是主題的功勞。
View類的后兩個構造函數都是與主題相關的,也就是說,在你自定義View時,如果不需要你的View隨着主題變化而變化,有前兩個構造函數就OK了,但是如果你想你的View隨着主題變化而變化,就需要利用后兩個構造函數了。
2 屬性賦值的優先級
當可以在多個地方賦值屬性時,一個問題就不可避免的出現了:優先級!!!
一個屬性可以在多個地方賦值,xml定義,xml中引入style,theme中直接指定,defStyleAttr,defStyleRes 這5個地方。(后面會將這幾個地方的用處)
【第二個問題】
屬性在多個地方被賦值后,系統以哪個屬性為准呢?
我將用一個實驗,利用多個TextView整體說明屬性賦值的優先級,這個實驗將貫穿文章后面,我將分塊講解。
【實驗】
首先我們定義一個style文件,從style中可以看出,我們定義了一個主題,主題中有兩種定義textView顏色的形式,一種是對textViewStyle進行定義(藍色),一種是直接對textColor進行定義(紫色)。這是主題運用的兩種方式,后面詳述,現在我們只需要知道,我們的主題默認不是白色的!!!!
后面的幾種style,每種對應一個顏色,用來區分優先級。
【實驗 - style文件】
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="@android:style/Theme.Holo">
<item name="android:textViewStyle">@style/BlueTextStyle</item>
<item name="android:textColor">@android:color/holo_purple</item>
</style>
<style name ="BlueTextStyle">
<item name="android:textColor">@android:color/holo_blue_light</item>
</style>
<style name ="RedTextStyle">
<item name="android:textColor">@android:color/holo_red_light</item>
</style>
<style name ="OrangeTextStyle">
<item name="android:textColor">@android:color/holo_orange_light</item>
</style>
<style name ="GreenTextStyle">
<item name="android:textColor">@android:color/holo_green_light</item>
</style>
</resources>
【實驗 - 布局文件】
我們聲明一個layout文件,里面有多個TextView和我自定義的View,現在我們自需要現在只看前三個TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Normal TextView"
android:textSize="30sp"/>
<TextView
style="@style/RedTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red style TextView"
android:textSize="30sp"/>
<TextView
style="@style/RedTextStyle"
android:textColor="@android:color/holo_orange_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XML defined orangeTextView"
android:textSize="30sp"/>
<com.zhaohui.code.view.CustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CustomView"
android:textSize="30sp"/>
<com.zhaohui.code.view.CustomBlankView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CustomBlankView !"
android:textSize="30sp"/>
<com.zhaohui.code.view.CustomGreenView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CustomGreenView !"
android:textSize="30sp"/>
<com.zhaohui.code.view.CustomGreenView
android:textColor="@android:color/holo_orange_light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Custom styled Red TextView !"
android:textSize="30sp"/>
<com.zhaohui.code.view.CustomGreenView
style="@style/RedTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Custom styled Red TextView !"
android:textSize="30sp"/>
</LinearLayout>
【實驗結果】
【實驗 - 結果分析1】
我們現在只看前三個TextView
第一個由於主題的原因,是藍色
第二個 style="@style/RedTextStyle",顏色是紅色,說明優先級style>theme
第三個style和xml定義同時存在,
style="@style/RedTextStyle
android:textColor="@android:color/holo_orange_light"
顯示 橙色,說明優先級xml定義>style>theme
因此我們得到結論1:
【結論1】:優先級xml定義>style>theme
【實驗 - 結果分析2】
但是theme的分析遠沒有結束,我們剛才在定義主題時,有兩個賦值,
<style name="AppTheme" parent="@android:style/Theme.Holo">
<item name="android:textViewStyle">@style/BlueTextStyle</item>
<item name="android:textColor">@android:color/holo_purple</item>
</style>
為什么TextView默認的顏色是藍色,而非紫色呢?????
這就需要研究View是如何利用系統主題,這時候需要回到我們今天的主題:View的構造函數!!!!
View中如何體現主題的信息,需要就通過實驗對View進行徹底探究。這是一個復雜的問題,需要看看View的第三和第四個構造函數,在看這兩個構造函數時,就不可避免的看到兩個讓人懵b的參數:defStyleAttr和defStyleRes。這時引入我們的第三個問題。
【第三個問題】
那么在View的第四個構造函數中的后面兩個的參數都是什么意思呢?
我們首先看看View的注釋:
* @param defStyleAttr An attribute in the current theme that contains a
* reference to a style resource that supplies default values for
* the view. Can be 0 to not look for defaults.</span><span style="background:rgb(255,255,255)">
第四個構造函數中第三個參數defStyleAttr,從名字就能看出,是一個屬性資源。
這個屬性資源跟主題有一個奇妙的協議:只要在主題中對這個屬性賦值,該View就會自動應用這個屬性的值。
再看看在第四個構造函數中有一個參數defStyleRes,這個參數是什么作用呢?
先看注釋:
@param defStyleRes A resource identifier of a style resource that
* supplies default values for the view, used only if
* defStyleAttr is 0 or can not be found in the theme. Can be 0
* to not look for defaults.</span>
這個參數說,只有在第三個參數defStyleAttr為0,或者主題中沒有找到這個defStyleAttr屬性的賦值時,才可以啟用。而且這個參數不再是Attr了,而是真正的style。其實這也是一種低級別的“默認主題”,即在主題未聲明屬性值時,我們可以主動的給一個style,使用這個構造函數定義出的View,其主題就是這個定義的defStyleRes(是一種寫死的style,因此優先級被調低)。
【源碼實例】
我們看一下在TextView中是如何給defStyleAttr和defStyleRes這兩個參數賦值的。查看TextView的源碼中,四個構造函數:
public TextView(Context context) {
this(context, null);
}
public TextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.textViewStyle);
}
public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
@SuppressWarnings("deprecation")
public TextView(
Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
......
}
可以看出,TextView的第2個構造函數直接調用了第3個構造函數,只是傳了一個com.android.internal.R.attr.textViewStyle的參數,第三個在調用第四個構造函數時,最后一個參數是0.
【TextView 的defStyleAttr和defStyleRes】
也就是說在TextView中,TextView的第二個構造函數傳入的defStyleAttr是com.android.internal.R.attr.textViewStyle。
那么這個com.android.internal.R.attr.textViewStyle是個什么鬼呢,我們從源碼中看看。
查看/Sdk/platforms/android-23/data/res/values這個路徑中找個一個attrs.xml文件,打開看看,找到textViewStyle,如下所示,哦,原來是一個reference類型的屬性,因此在給這個屬性賦值時,在xml中一般使用@style/xxx形式就可以了。
<declare-styleable name="Theme">
......
<attr name="textViewStyle" format="reference"/>
......
</declare-styleable>
app的主題可以為這個textViewStyle屬性提供一套默認的style資源。比如在本例中,我們的主題繼承自Theme.Holo中,在Theme.Holo中有一個item如下:
<item name="textViewStyle">@style/Widget.Holo.TextView</item>
說明在Theme.Holo中,textViewStyle指向@style/Widget.Holo.TextView
因此默認情況下,TextView的屬性都是在Widget.Holo.TextView這個Style中(但是其實這個style中並沒有對textColor進行定義,有興趣的可以自己去看看)。
在本例中我們自己定義了主題,通過繼承Theme.Holo主題,修改這個textViewStyle的reference,使得textViewStyle指向了藍色主題,如下所示。因此本文中app的TextView默認顏色是藍色。
<style name="AppTheme" parent="@android:style/Theme.Holo">
<item name="android:textViewStyle">@style/BlueTextStyle</item>
<item name="android:textColor">@android:color/holo_purple</item>
</style>
但是,我們的主題的內容並沒有完結,很明顯,我們在主題中還有一個android:textColor的賦值。
在同時使用了defStyleAttr(即主題中定義的textViewStyle)和主題直接定義時,顯示了defStyleAttr的定義,說明使用了defStyleAttr的優先級要比直接在主題中聲明優先級高。因此我們又得到一個結論。
【結論2】:優先級 defStyleAttr>theme直接定義
【第四個問題】
從上文中我們知道了defStyleAttr和theme的順序,那么defStyleRes的優先級呢?
現在需要確定defStyleRes的優先級了,我們重新回到我們的實驗,我們的實驗里,構造了三種自定義的View, CustomView, CustomBlankView和CustomGreenView。
CustomView的代碼上面已寫,現在將剩下兩種自定義的View代碼展示如下:
【實驗 - CustemGreenView類】
package com.zhaohui.code.view;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
import com.example.zhaohui.player.R;
/**
* Created by zhaohui on 16-8-12.
*/
public class CustomGreenView extends TextView {
String tag = "customGreen";
public CustomGreenView(Context context) {
super(context);
}
public CustomGreenView(Context context, AttributeSet attrs) {
this(context,attrs,0);
}
public CustomGreenView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr,R.style.GreenTextStyle);
}
public CustomGreenView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr,defStyleRes);
}
}
【實驗 - CustomBlankView 類】
package com.zhaohui.code.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by zhaohui on 16-8-12.
*/
public class CustomBlankView extends TextView {
String tag = "customGreen";
public CustomBlankView(Context context) {
super(context);
}
public CustomBlankView(Context context, AttributeSet attrs) {
this(context,attrs,0);
}
public CustomBlankView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr,0);
}
public CustomBlankView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr,defStyleRes);
}
}
【實驗-結果分析3】
在CustomGreenView中,defStyleAttr被賦值為0,defStyleRes賦值為R.style.GreenTextStyle,即我們的綠色style。
在CustomBlankView中,defStyleAttr和defStyleRes都為0,此時的顏色是紫色,即直接在theme中聲明的顏色。
說明在同時在defStyleRes和主題中聲明時,優先顯示defStyleRes.由此又得出一個結論。
【結論3】:優先級 defStyleRes>theme直接定義
在實驗的最后兩個還是說明了直接在xml中寫屬性的優先級較高,即
【結論4】:優先級 xml直接定義>xml的style定義>theme直接定義
【小結】
在Theme中的優先級主要涉及到三個部分:defStyleAttr,defStyleRes和主題直接定義
我們需要分三種情況,在構造函數中,
1 當defStyleAttr!=0時,
主題中如果對defStyleAttr屬性進行賦值,顯示對defStyleAttr的賦值,優先級最高!
2 當(defStyleAttr==0或主題沒有對defStyleAttr進行賦值)&& defStyleRes!=0而且theme中沒有定義時時,顯示defStyleRes,優先級中
3 如果defStyleAttr==0且defStyleRes==0時,顯示theme直接定義,優先級最低
由此我們得到屬性賦值總體優先級:
【結論 總】屬性賦值優先級 Xml定義 > xml的style定義 > defStyleAttr > defStyleRes> theme直接定義
四 總結
在View類中有四個構造函數,涉及到多個參數,
Context:上線文,這個不用多說
AttributeSet attrs: 從xml中定義的參數
int defStyleAttr :主題中優先級最高的屬性
int defStyleRes : 優先級次之的內置於View的style
在android中的屬性可以在多個地方進行賦值,涉及到的優先級排序為:
Xml直接定義 > xml中style引用 > defStyleAttr > defStyleRes > theme直接定義
總體來說,本文是對android中View的四個構造函數的探究,主要涉及到View屬性的優先級問題,希望大家發現問題或不足時及時跟我聯系。
————————————————
版權聲明:本文為CSDN博主「學渣的第六感」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/zhao123h/article/details/52210732
