假设app的title如下
假设app的title 统一的都是这种左中右结构的 代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/app_title_style" android:baselineAligned="false" android:gravity="center_vertical" android:orientation="horizontal"> <ViewSwitcher android:id="@+id/app_title_left_switcher" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/app_title_left_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回"/> <ImageView android:id="@+id/app_title_left_image" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </ViewSwitcher> <ViewSwitcher android:id="@+id/app_title_middle_switcher" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_marginLeft="16dip" android:layout_weight="1"> <TextView android:id="@+id/app_title_middle_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="title"/> <ImageView android:id="@+id/app_title_middle_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"/> </ViewSwitcher> <ViewSwitcher android:id="@+id/app_title_right_switcher" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/app_title_right_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一步"/> <ImageView android:id="@+id/app_title_right_image" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </ViewSwitcher> </LinearLayout>
先来继续完善一下BaseActivity
protected void onCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); ActivityMgr.push(this); findViewById(); } // 初始化app中通用的控件 protected void findViewById(){ } // 设置标题栏 protected void setTitle(){ }
然后看一下BaseActivity的具体实现类TitleDemoActivity
public class TitleDemoActivity extendsBaseActivity{ protectedvoid onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); } protectedvoid findViewById(){ setContentView(R.layout.title_demo); super.findViewById(); super.setTitle();// 设置标题栏 } }
BaseActivity的设计初衷是所有的Activity的都继承该类。
首先定义一些通用的属性、以及方法
private ViewSwitcher mLeftSwitcher; private ViewSwitcher mMiddleSwitcher; private ViewSwitcher mRightSwitcher; /** * 初始化View */ protected void findViewById() { mLeftSwitcher = (ViewSwitcher) findViewById(R.id.app_title_left_switcher); mMiddleSwitcher = (ViewSwitcher) findViewById(R.id.app_title_middle_switcher); mRightSwitcher = (ViewSwitcher) findViewById(R.id.app_title_right_switcher); } protected void setTitle(String left, String middle, String right) { ((TextView) mLeftSwitcher.getChildAt(0)).setText(left); ((TextView) mMiddleSwitcher.getChildAt(0)).setText(middle); ((TextView) mRightSwitcher.getChildAt(0)).setText(right); }
public class TitleDemoActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override protected void findViewById() { setContentView(R.layout.title_demo); super.findViewById(); setTitle("返回主页", "这是一个Title", "下一个界面"); } }