android SDK开发 -- TitleBar封装(一)

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

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值