在此之前,使用过几种方法设置标题栏:

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定制标题栏的更多相关文章

  1. 两种方法一句代码隐藏Activity的标题栏

    把Activity的标题栏隐藏有两种方法.一种是在在Activity里面设置javacode.还有一种是在项目的清单文件AndroidManifest.xml中设置模版样式. 一.在Activity中 ...

  2. activity去标题栏操作&保留高版本主题

    方式一:每个类都需要去添加此代码 在setContentView(R.layout.activity_splash); 前设置以下代码 requestWindowFeature(Window.FEAT ...

  3. Android 自定义Activity的标题栏(Titlebar)

    缺省的情况下,通常见到Activity的标题栏(Titlebar)是这样的(红色框内): HandleContacts是Activity的标题.有时候,我们希望能改变一下这样单调的状况.比如,要在标题 ...

  4. Android——Activity去除标题栏和状态栏

    一.在代码中设置 public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  //去 ...

  5. Android Activity去除标题栏和状态栏

    一.在代码中设置public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //去除ti ...

  6. 炫酷:一句代码实现标题栏、导航栏滑动隐藏。ByeBurger库的使用和实现

    本文已授权微信公众号:鸿洋(hongyangAndroid)原创首发. 其实上周五的时候已经发过一篇文章.基本实现了底部导航栏隐藏的效果.但是使用起来可能不是很实用.因为之前我实现的方式是继承了系统的 ...

  7. android第一行代码-2.activity基本用法

    摘要: 本节主要涉及到的有activity的创建,标题栏隐藏,button绑定方法(toast的使用),menu使用,活动销毁 1.activity的创建跟注册 创建: public class Te ...

  8. Android Activity 去掉标题栏及全屏显示

    默认生成的活动(Activity)界面中包含标题栏,并带有状态栏.有时不需要这两个控件. 1.去掉标题栏 (三种方法) a:在setContentView()方法前 添加:requestWindowF ...

  9. Android - Activity定制横屏(landscape)显示

    Activity定制横屏(landscape)显示 本文地址: http://blog.csdn.net/caroline_wendy Android横屏(landscape)显示:  android ...

随机推荐

  1. Vi/Vim查找替换使用方法【转】

    原文地址:http://wzgyantai.blogbus.com/logs/28117977.html vi/vim 中可以使用 :s 命令来替换字符串.该命令有很多种不同细节使用方法,可以实现复杂 ...

  2. ios app抓包分析

    1 使用rvictl工具 这是mac下的一条命令.ios usb连mac,然后创建虚拟网络接口. 2 使用wireshark抓包 wireshark可以抓这个虚拟网络接口上的数据包.

  3. 磁盘扩容 磁盘阵列(Redundant Arrays of Independent Disks,RAID)

    磁盘阵列(Redundant Arrays of Independent Disks,RAID) 云 500G  不够用 扩容 方案1  重建分区,由500G到1T,按历史增速,1年后再扩到1.5T, ...

  4. IntentFilter打印方法

    转载请注明出处:http://blog.csdn.net/droyon 在我们进行Android应用程序开发时.我们有时须要对某个对象进行打印输出.以方便我们进行调试. 非常多对象实现了toStrin ...

  5. bat文件中调用传参的问题

    https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-lin ...

  6. TP5.x——开启跨域访问

    前言 其实很简单,在入口文件的index.php添加几句header就可以了. 代码 public/index.php header("Access-Control-Allow-Origin ...

  7. 【HDU 1520】 Anniversary Party

    [题目链接] 点击打开链接 [算法] 树形DP 令f[i][0]表示 : 以i为根的子树中,若i不参加宴会,所能获得的最大愉悦值 f[i][1]表示 : 以i为根的子树中,若i参加宴会,所能获得的最大 ...

  8. Linux中的工作队列

    工作队列(work queue)是Linux kernel中将工作推后执行的一种机制.这种机制和BH或Tasklets不同之处在于工作队列是把推后的工作交由一个内核线程去执行,因此工作队列的优势就在于 ...

  9. bzoj 4711 小奇挖矿 —— 树形DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4711 就是树形DP,然而也想了半天才把转移想清楚: f[x][j][0] 表示 x 去上面 ...

  10. WebLogic之eclipse安装WebLogic插件

    转自:https://blog.csdn.net/magi1201/article/details/38323775