DecorView,androiddecorview

來源:互聯網
上載者:User

DecorView,androiddecorview

一、DecorView為整個Window介面的最頂層View。

二、DecorView只有一個子項目為LinearLayout。代表整個Window介面,包含通知欄,標題列,內容顯示欄三塊地區。

三、LinearLayout裡有兩個FrameLayout子項目。

  (20)為標題列顯示介面。只有一個TextView顯示應用的名稱。也可以自訂標題列,載入後的自訂標題列View將加入FrameLayout中。

  (21)為內容欄顯示介面。就是setContentView()方法載入的布局介面,加入其中。

工具查看:

1.

為SDK中tools檔案夾下hierarchyviewer bat 查看ViewTree的結果:

(此時未替換標題列)

 

2.替換標題列後ViewTree的變化:

綠色地區發生了變化,改變為了載入的title.xml檔案的布局。

title.xml內容為:

123456789101112131415161718 <?xml version="1.0"encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent">  <ImageView    android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:src="@drawable/icon2"/>  <TextView      android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:id="@+id/title_tv"  android:textColor="#FFFFFF"  android:textStyle="bold"  android:text="@string/app_name"  /></LinearLayout>


 

通知欄繪製在1號LinearLayout中,還是繪製在DecorView中還有待探究。

-----------------

ApiDemo中app包下CustomTitle中自訂TitleBar程式碼片段

123 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);setContentView(R.layout.custom_title);getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);


 

載入自訂titleBar後如何尋找其中元素呢,其實還是findViewById就可以了,因為載入的自訂布局已經在DecorView樹中了

而findViewById是怎麼回事呢。

activity中findViewById源碼

123 public View findViewById(int id) {    returngetWindow().findViewById(id);}


調用了getWindow().findViewById,getWindow返回的是Window點入查看window中源碼

123 public View findViewById(int id) {    returngetDecorView().findViewById(id);}


所以最終是從DecorView最頂層開始搜尋的。

下面這篇文章可能會協助加深理解:

Android 中Activity,Window和View之間的關係 http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0322/1055.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.