返回介绍

TitleBar 封装(一)

发布于 2025-02-25 12:45:30 字数 3893 浏览 0 评论 0 收藏 0

假设 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();// 设置标题栏
     }
}

TitleBar 封装

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", "下一个界面");
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文