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

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. 嵌入式开发之davinci--- 8148/8168/8127 中的图像缩放sclr、swms之后出现图像视频卡顿、屏幕跳跃的问题

    ()问题原因 这边的case链路是这样的camera->sclr(yuv420sp cif)->dup->ipcframeoutm3<->ipcframerocess&l ...

  2. STL_算法_最小值和最大值(min_element、max_element)

    C++ Primer 学习中... 简单记录下我的学习过程 (代码为主) min_element.max_element  找最小.最大值. 非常easy没什么大作用 #include<iost ...

  3. [思考]我们应该怎样建设企业IT

    从人员架构上来看,要不要企业自己的IT团队?如果要,应该有什么样的架构?运维,开发,管理,项目? 从是否外包角度看,要不要外包?如果外包,如何建立外包流程? 从业务角度看,企业IT的发展应该是围绕业务 ...

  4. 安全相关的head头

    与安全相关的head头包括 参考网站:https://developer.mozilla.org/en-US/docs/Web/HTTP Content-Security-Policy(CSP):禁止 ...

  5. C# 敏感词过滤

    public class BadWordFilter { #region 变量 private HashSet<string> hash = new HashSet<string&g ...

  6. 网页音乐播放器javascript实现,可以显示歌词

    可以显示歌词,但是歌词和歌曲都要实现自己下载下来.只能播放一首歌,歌词还得是lrc格式的代码写的很罗嗦,急切希望帮改改CSS的代码​1.代码:<html >    <head> ...

  7. 本地锁、redis分布式锁、zk分布式锁

    本地锁.redis分布式锁.zk分布式锁 https://www.cnblogs.com/yjq-code/p/dotnetlock.html 为什么要用锁? 大型站点在高并发的情况下,为了保持数据最 ...

  8. hdu 1719

    Friend Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  9. vs 中switch语句快捷键列出枚举

    先switch然后两下tab会补完到default,光标显示在switch后的变量这时输入枚举,输完后回车,自动补完所有枚举的case

  10. asp.net mvc4 controller

    controller返回几种返回结果