在此之前,使用過幾種方法設置標題欄:
1.常規法:這個方法是最常用的了,哪個activity需要什么樣的標題欄,就在對應的xml布局設計。缺點:此方法維護起來困難,沒有將標題欄的共性抽取出來,
如果要統一修改所有activity的標題欄的背景顏色,這將是一個不小的工作量;
2.自定義控件:標題欄一般包含了左邊的返回鍵,中間的標題,有時右邊會有“保存”的TextView,自定義TextView,把這幾個需要的控件封裝成一個View,
暴露設置標題、點擊事件等方法。此方法的缺點,就是必須在需要用到的xml中添加此自定義view;
3.抽象方法:創建activity的基類,基類的布局就單純的包含了標題欄,在不同的子類去擴展。缺點:基類必須獨立,如果基類包含了其他的屬性(如關閉動畫),
那么此基類並不適合於所有的activity。
因此,抽空寫了一個管理類,將標題欄寫在特定的布局,用特定的類進行封裝,並暴露對應的方法給調用。此外,管理類還有一個功能,將標題欄和內容布局合並
在一起,使用者(activity)完全只需要一句代碼即可完成。
1.創建標題欄布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ac_base_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/top_bar_height"
android:background="@color/city_tabbar_color" >
<LinearLayout
android:id="@+id/ll_ac_base_toolbar_left"
android:layout_width="@dimen/delivery"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:clickable="true"
android:gravity="center_vertical"
android:onClick="closingEntrustSendActivity" >
<ImageView
android:id="@+id/iv_ac_base_toolbar_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/topbar_img_left"
android:visibility="gone" />
<TextView
android:id="@+id/tv_ac_base_toolbar_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/topbar_img_left"
android:clickable="true"
android:textColor="@color/ll_default_color"
android:textSize="@dimen/me_tv_size"
android:visibility="gone" />
</LinearLayout>
<TextView
android:id="@+id/tv_ac_base_toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@color/ll_default_color"
android:textSize="@dimen/max_size" />
<LinearLayout
android:id="@+id/ll_ac_base_toolbar_right"
android:layout_width="@dimen/delivery"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:clickable="true"
android:gravity="center_vertical" >
<TextView
android:id="@+id/tv_ac_base_toolbar_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/topbar_img_left"
android:clickable="true"
android:textColor="@color/ll_default_color"
android:textSize="@dimen/me_tv_size"
android:visibility="gone" />
<ImageView
android:id="@+id/iv_ac_base_toolbar_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/topbar_img_left"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
2.創建管理類








3.activity調用

R.layout.activity_policy_type這個布局就是activity本身需要的布局。實現的原理很簡單,創建這個activity的一個LinearLayout,把標題欄布局和內容顯示的布局添加上去,然后把
標題欄的一些屬性設置暴露方法給調用。每個activity需要用到的標題欄只需簡單的幾行代碼即可。不過也是有一定的缺點,比如頁面內容豐富的情況下,頁面出現過度渲染。
