Android一句代码给Activity定制标题栏
在此之前,使用过几种方法设置标题栏:
1.常规法:这个方法是最常用的了,哪个activity需要什么样的标题栏,就在对应的xml布局设计。缺点:此方法维护起来困难,没有将标题栏的共性抽取出来,
如果要统一修改所有activity的标题栏的背景颜色,这将是一个不小的工作量;
2.自定义控件:标题栏一般包含了左边的返回键,中间的标题,有时右边会有“保存”的TextView,自定义TextView,把这几个需要的控件封装成一个View,
暴露设置标题、点击事件等方法。此方法的缺点,就是必须在需要用到的xml中添加此自定义view;
3.抽象方法:创建activity的基类,基类的布局就单纯的包含了标题栏,在不同的子类去扩展。缺点:基类必须独立,如果基类包含了其他的属性(如关闭动画),
那么此基类并不适合于所有的activity。
因此,抽空写了一个管理类,将标题栏写在特定的布局,用特定的类进行封装,并暴露对应的方法给调用。此外,管理类还有一个功能,将标题栏和内容布局合并
在一起,使用者(activity)完全只需要一句代码即可完成。
1.创建标题栏布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ac_base_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/top_bar_height"
android:background="@color/city_tabbar_color" >
<LinearLayout
android:id="@+id/ll_ac_base_toolbar_left"
android:layout_width="@dimen/delivery"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:clickable="true"
android:gravity="center_vertical"
android:onClick="closingEntrustSendActivity" >
<ImageView
android:id="@+id/iv_ac_base_toolbar_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/topbar_img_left"
android:visibility="gone" />
<TextView
android:id="@+id/tv_ac_base_toolbar_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/topbar_img_left"
android:clickable="true"
android:textColor="@color/ll_default_color"
android:textSize="@dimen/me_tv_size"
android:visibility="gone" />
</LinearLayout>
<TextView
android:id="@+id/tv_ac_base_toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@color/ll_default_color"
android:textSize="@dimen/max_size" />
<LinearLayout
android:id="@+id/ll_ac_base_toolbar_right"
android:layout_width="@dimen/delivery"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:clickable="true"
android:gravity="center_vertical" >
<TextView
android:id="@+id/tv_ac_base_toolbar_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/topbar_img_left"
android:clickable="true"
android:textColor="@color/ll_default_color"
android:textSize="@dimen/me_tv_size"
android:visibility="gone" />
<ImageView
android:id="@+id/iv_ac_base_toolbar_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/topbar_img_left"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
2.创建管理类








3.activity调用

R.layout.activity_policy_type这个布局就是activity本身需要的布局。实现的原理很简单,创建这个activity的一个LinearLayout,把标题栏布局和内容显示的布局添加上去,然后把
标题栏的一些属性设置暴露方法给调用。每个activity需要用到的标题栏只需简单的几行代码即可。不过也是有一定的缺点,比如页面内容丰富的情况下,页面出现过度渲染。
Android一句代码给Activity定制标题栏的更多相关文章
- 两种方法一句代码隐藏Activity的标题栏
把Activity的标题栏隐藏有两种方法.一种是在在Activity里面设置javacode.还有一种是在项目的清单文件AndroidManifest.xml中设置模版样式. 一.在Activity中 ...
- activity去标题栏操作&保留高版本主题
方式一:每个类都需要去添加此代码 在setContentView(R.layout.activity_splash); 前设置以下代码 requestWindowFeature(Window.FEAT ...
- Android 自定义Activity的标题栏(Titlebar)
缺省的情况下,通常见到Activity的标题栏(Titlebar)是这样的(红色框内): HandleContacts是Activity的标题.有时候,我们希望能改变一下这样单调的状况.比如,要在标题 ...
- Android——Activity去除标题栏和状态栏
一.在代码中设置 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //去 ...
- Android Activity去除标题栏和状态栏
一.在代码中设置public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //去除ti ...
- 炫酷:一句代码实现标题栏、导航栏滑动隐藏。ByeBurger库的使用和实现
本文已授权微信公众号:鸿洋(hongyangAndroid)原创首发. 其实上周五的时候已经发过一篇文章.基本实现了底部导航栏隐藏的效果.但是使用起来可能不是很实用.因为之前我实现的方式是继承了系统的 ...
- android第一行代码-2.activity基本用法
摘要: 本节主要涉及到的有activity的创建,标题栏隐藏,button绑定方法(toast的使用),menu使用,活动销毁 1.activity的创建跟注册 创建: public class Te ...
- Android Activity 去掉标题栏及全屏显示
默认生成的活动(Activity)界面中包含标题栏,并带有状态栏.有时不需要这两个控件. 1.去掉标题栏 (三种方法) a:在setContentView()方法前 添加:requestWindowF ...
- Android - Activity定制横屏(landscape)显示
Activity定制横屏(landscape)显示 本文地址: http://blog.csdn.net/caroline_wendy Android横屏(landscape)显示: android ...
随机推荐
- Ant中批量调用TestNG的XML文件,并调用TestNgXlst生成漂亮的html测试报告
from:http://blog.csdn.net/bwgang/article/details/7865184 1.在Ant中设置如下: <target name="run_test ...
- web 开发之js---ajax 中的两种提交方式ajax post 和 ajax get 实例
()post http://04101334.iteye.com/blog/637695/ ()get function serializeElement(element) { var method ...
- Python标准库:内置函数complex([real[, imag]])
本函数能够使用參数real + imag*j方式创建一个复数.也能够转换一个字符串的数字为复数:或者转换一个数字为复数.假设第一个參数是字符串,第二个參数不用填写.会解释这个字符串且返回复数.只是,第 ...
- win7 32位下安装MySQL出现的---1067系统错误---问题及解决
每次安装数据库,总是出现这样那样的问题.如今记录下来,供日后參考咯.... 下载的是解压缩-zip版本号的.安装配置教程參照洪哥笔记文章-<MySQL-5.6.13解压版(zip版)安装配置教程 ...
- 识别IE11浏览器
现在俺们做的系统十分高大上,用IE的话非要上IE11或以上版本. 咋检测呢?检测到用户用IE.且IE低于IE11的话就提示他升级浏览器呢?可以酱紫: var _IE = (function (d, w ...
- 2016/3/31 拾遗 php字符串中 转义字符 “ ’‘ ” ’ “” ‘ " \’ ' ' \‘ " " \" '' \ " " 使用
<?php echo $str_string1='甲问:"你在哪里学的PHP?"'; echo "<br />"; echo $str_str ...
- Delphi汉字简繁体转换代码(分为D7和D2010版本)
//delphi 7 Delphi汉字简繁体转换代码unit ChineseCharactersConvert; interface uses Classes, Windows; type T ...
- Qt 学习之路 2(19):事件的接受与忽略(当重写事件回调函数时,时刻注意是否需要通过调用父类的同名函数来确保原有实现仍能进行!有好几个例子。为什么要这么做?而不是自己去手动调用这两个函数呢?因为我们无法确认父类中的这个处理函数有没有额外的操作)
版本: 2012-09-29 2013-04-23 更新有关accept()和ignore()函数的相关内容. 2013-12-02 增加有关accept()和ignore()函数的示例. 上一章我们 ...
- 476. Number Complement(补数)
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- AOP日志框架实现
AOP日志框架实现 JDK动态代理实现日志框架 首先,在项目包com.ay.test 下创建业务接口类BusinessClassService,具体代码如下: BusinessC lassServic ...