Android ApiDemo示例解读5:Activity->Custom Title

Android UI缺省的标题栏由android:label 定义,显示在屏幕左上角,Android允许Activity自定义标题栏,使用自定义Layout重新设置标题栏,比如实现Windows Mobile 风格的标题栏。

App->Activity->Custom Title 重新将Activity标题栏定义为左右两个文本框,其Layout定义R.layout.custom_title_1如下:

(下面的xml中包含了一左一右两个文本框。

与每个Activity对应的除了使用由setContentView设置的Content View之外,还有一个Windows类对象,Windows 类对象用于控制标题栏,可以允许自定义标题栏或是不显示标题栏。)

 
 
 
  1. <RelativeLayout   
  2.  xmlns:android=”http://schemas.android.com/apk/res/android”   
  3.  android:id=”@+id/screen”   
  4.  android:layout_width=”match_parent”   
  5.  android:layout_height=”match_parent”   
  6.  android:orientation=”vertical”>   
  7.  <TextView android:id=”@+id/left_text”   
  8.  android:layout_width=”wrap_content”   
  9.  android:layout_height=”wrap_content”   
  10.  android:layout_alignParentLeft=”true”   
  11.  android:text=”@string/custom_title_left” />   
  12.  <TextView android:id=”@+id/right_text”   
  13.  android:layout_width=”wrap_content”   
  14.  android:layout_height=”wrap_content”   
  15.  android:layout_alignParentRight=”true”   
  16.  android:text=”@string/custom_title_right” />   
  17.  </RelativeLayout>   

Windows 定义了一些Feature,允许开发人员做些定制:自定义标题栏对应的Feature ID为Window.FEATURE_CUSTOM_TITLE。

 
 
 
  1. requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);   
  2. setContentView(R.layout.custom_title);   
  3. getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);   

 

THE END