博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android SDK开发 -- TitleBar封装(一)
阅读量:7029 次
发布时间:2019-06-28

本文共 2292 字,大约阅读时间需要 7 分钟。

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

1 
6
10
15
19
20
26
32
37
38
42
47
51
52

 

 

 

 

先来继续完善一下BaseActivity

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

 

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

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

 

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 }

 

 

子类调用

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

 

转载地址:http://mkgxl.baihongyu.com/

你可能感兴趣的文章
HTML简单的注册页面搭建
查看>>
【06】Vue 之 组件化开发
查看>>
Docker 安装
查看>>
多数据库数据导入
查看>>
[AVR]高压并行编程---基础知识
查看>>
inl文件介绍
查看>>
前端坑--表单篇
查看>>
P2P原理基础
查看>>
完成登录功能,用session记住用户名
查看>>
DBCP和C3P0使用--未完善
查看>>
JS常用方法(获取Class、获取元素样式、事件监听、cookie、ajax等)
查看>>
BZOJ 1084 最大子矩阵
查看>>
2018杭电多校第三场1007(凸包,极角排序)
查看>>
django中orm的简单操作
查看>>
Mybatis知识(1)
查看>>
[CentOS] 7 不执行文件 /etc/rc.d/rc.local
查看>>
模态窗口的各个属性
查看>>
10.28 (上午) 开课一个月零二十四天 (数据访问)
查看>>
为什么你应该(从现在开始就)写博客
查看>>
小技巧积累
查看>>