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 ...
随机推荐
- 连接sql2008时报错
最近把公司的项目搭建到本地(周末回家要加班),可是连接后,发现程序后台出错,错误信息:不支持此服务器版本.目标服务器必须是 SQL Server 2000 或更高版本. 本地是SqlServer200 ...
- 初步学习C++中的继承关系
继承机制是面向对象程序设计使代码能够复用的最重要的手段,它同意程序猿在保持原有类特性的基础上进行扩展,添加功能. 这样产生新的类,称派生类.继承呈现了面向对象程序设计的层次结构,体现了由简单到复杂 ...
- mp3 pcm
mp3 pcm javaMP3转pcm 百度语音识别 - 且学且珍惜 - SegmentFault 思否 https://segmentfault.com/a/1190000013383967
- MySQL 5.7 Keywords and Reserved Words
https://dev.mysql.com/doc/refman/5.7/en/keywords.html#keywords-5-7-detailed-T
- jsp实现翻页功能
jsp实现翻页功能 要实现翻页功能,只需要设置一个pageIndex即可,然后每次加载页面时通过pageIndex去加载数据就行. 那么我们可以设置一个隐藏的input框,用于传递pageIndex给 ...
- HDU - 4333 Revolving Digits(拓展kmp+最小循环节)
1.给一个数字字符串s,可以把它的最后一个字符放到最前面变为另一个数字,直到又变为原来的s.求这个过程中比原来的数字小的.相等的.大的数字各有多少. 例如:字符串123,变换过程:123 -> ...
- pssh 批量管理执行
pssh 是一个python写的批量执行工具,非常适合30台服务器以内的一些重复性的操作 安装很简单,只要python版本2.4 以上的都行 用这个工作最好把机器做做好ssh信任关系,不然很麻烦 每次 ...
- 微信公众号开发——创建自定义菜单(PHP版)
<?php include "TokenUtil.php"; //TokenUtil::build_access_token(); $access_token = Token ...
- HDU 2914 Triangle (Fibnacci 数)
题意:给你一个长度为 n 的木棒,求至少拿掉几根使得剩余的木棒构成不了三角形. 析:为了保证不形成三角形,所以保证两边之和等于最大边是最优,这不就是Fibnacci 数么,由于 n 很小,if-els ...
- HDU5996:dingyeye loves stone
题目链接:dingyeye loves stone 题意:给出一棵树,树上的每个节点都有石子若干, 两人博弈,每次操作都可以把任意节点的任意石子数转移到它的父亲节点, 若无法操作则输,给出树上的节点及 ...