假设app的title如下

假设app的title 统一的都是这种左中右结构的 代码如下

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. style="@style/app_title_style"
  3. android:baselineAligned="false"
  4. android:gravity="center_vertical"
  5. android:orientation="horizontal">
  6. <ViewSwitcher
  7. android:id="@+id/app_title_left_switcher"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content">
  10. <TextView
  11. android:id="@+id/app_title_left_text"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="返回"/>
  15. <ImageView
  16. android:id="@+id/app_title_left_image"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"/>
  19. </ViewSwitcher>
  20. <ViewSwitcher
  21. android:id="@+id/app_title_middle_switcher"
  22. android:layout_width="0dip"
  23. android:layout_height="wrap_content"
  24. android:layout_marginLeft="16dip"
  25. android:layout_weight="1">
  26. <TextView
  27. android:id="@+id/app_title_middle_text"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_gravity="center"
  31. android:text="title"/>
  32. <ImageView
  33. android:id="@+id/app_title_middle_image"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_gravity="center"/>
  37. </ViewSwitcher>
  38. <ViewSwitcher
  39. android:id="@+id/app_title_right_switcher"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content">
  42. <TextView
  43. android:id="@+id/app_title_right_text"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:text="下一步"/>
  47. <ImageView
  48. android:id="@+id/app_title_right_image"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"/>
  51. </ViewSwitcher>
  52. </LinearLayout>

先来继续完善一下BaseActivity

  1. protected void onCreate(BundlesavedInstanceState){
  2. super.onCreate(savedInstanceState);
  3. ActivityMgr.push(this);
  4. findViewById();
  5. }
  6. // 初始化app中通用的控件
  7. protected void findViewById(){
  8. }
  9. // 设置标题栏
  10. protected void setTitle(){
  11. }

然后看一下BaseActivity的具体实现类TitleDemoActivity

  1. public class TitleDemoActivity extendsBaseActivity{
  2. protectedvoid onCreate(Bundle savedInstanceState){
  3. super.onCreate(savedInstanceState);
  4. }
  5. protectedvoid findViewById(){
  6. setContentView(R.layout.title_demo);
  7. super.findViewById();
  8. super.setTitle();// 设置标题栏
  9. }
  10. }

TitleBar封装

BaseActivity的设计初衷是所有的Activity的都继承该类。

首先定义一些通用的属性、以及方法

  1. private ViewSwitcher mLeftSwitcher;
  2. private ViewSwitcher mMiddleSwitcher;
  3. private ViewSwitcher mRightSwitcher;
  4. /** * 初始化View */
  5. protected void findViewById() {
  6. mLeftSwitcher = (ViewSwitcher) findViewById(R.id.app_title_left_switcher);
  7. mMiddleSwitcher = (ViewSwitcher) findViewById(R.id.app_title_middle_switcher);
  8. mRightSwitcher = (ViewSwitcher) findViewById(R.id.app_title_right_switcher);
  9. }
  10. protected void setTitle(String left, String middle, String right) {
  11. ((TextView) mLeftSwitcher.getChildAt(0)).setText(left);
  12. ((TextView) mMiddleSwitcher.getChildAt(0)).setText(middle);
  13. ((TextView) mRightSwitcher.getChildAt(0)).setText(right);
  14. }
  15.  

子类调用

  1. public class TitleDemoActivity extends BaseActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. }
  6. @Override
  7. protected void findViewById() {
  8. setContentView(R.layout.title_demo);
  9. super.findViewById();
  10. setTitle("返回主页", "这是一个Title", "下一个界面");
  11. }
  12. }