Android ApiDemo示例解讀5:Activity->Custom Title
Android UI缺省的標(biāo)題欄由android:label 定義,顯示在屏幕左上角,Android允許Activity自定義標(biāo)題欄,使用自定義Layout重新設(shè)置標(biāo)題欄,比如實(shí)現(xiàn)Windows Mobile 風(fēng)格的標(biāo)題欄。
App->Activity->Custom Title 重新將Activity標(biāo)題欄定義為左右兩個(gè)文本框,其Layout定義R.layout.custom_title_1如下:
(下面的xml中包含了一左一右兩個(gè)文本框。
與每個(gè)Activity對(duì)應(yīng)的除了使用由setContentView設(shè)置的Content View之外,還有一個(gè)Windows類對(duì)象,Windows 類對(duì)象用于控制標(biāo)題欄,可以允許自定義標(biāo)題欄或是不顯示標(biāo)題欄。)
- <RelativeLayout
- xmlns:android=”http://schemas.android.com/apk/res/android”
- android:id=”@+id/screen”
- android:layout_width=”match_parent”
- android:layout_height=”match_parent”
- android:orientation=”vertical”>
- <TextView android:id=”@+id/left_text”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:layout_alignParentLeft=”true”
- android:text=”@string/custom_title_left” />
- <TextView android:id=”@+id/right_text”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:layout_alignParentRight=”true”
- android:text=”@string/custom_title_right” />
- </RelativeLayout>
Windows 定義了一些Feature,允許開發(fā)人員做些定制:自定義標(biāo)題欄對(duì)應(yīng)的Feature ID為Window.FEATURE_CUSTOM_TITLE。
- requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
- setContentView(R.layout.custom_title);
- getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);