文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
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", "下一个界面");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论