Android DecorView淺析


一、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內容為:

<?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代碼段

        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源碼

    public View findViewById(int id) {
        return getWindow().findViewById(id);
    }

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

    public View findViewById(int id) {
        return getDecorView().findViewById(id);
    }

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

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

 

自定義TitleBar可以看這篇文章 http://blog.csdn.net/fulianwu/article/details/6834565 很詳細


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM