有意思的一点是,这些特效并不是你想设置的时候就行设置,你需要在Activity显示之前向系统申请要显示的特效,这样才能在下面的程序中为这些特效进行设置。
为一个Activity设置自定义Title的流程:
1)为自定义的Title建立一个layout(custom_title_1.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen"
android:layout_width="fill_parent" android:layout_height="fill_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="Left" />
<TextView android:id="@+id/right_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Right" />
</RelativeLayout>
2)为activity设定自定义Title特效并指定Title的layout:
在onCreate()事件中增加:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.custom_title);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);
这三条语句的次序不能颠倒,依次为申请特效,创建view,设置特效属性。其中requestWindowFeature等价于getWindow().requestFeature()
3)在需要修改Title的地方,获取left_text或者right_text进行设置即可。
Window还有其他一些feature,比如FEATURE_CONTEXT_MENU,FEATURE_NO_TITLE,FEATURE_LEFT_ICON等。