文章转自 CoderAbsolom  的 http://blog.csdn.net/qq_14923661/article/details/52003447

写在前面:

标题栏这个部分,绝大多数App都有,起到一个概括本页面功能的作用,有些标题栏还具有返回上一页等额外功能。

在这里不得不表扬一下苹果的标题栏位设计,简洁美观实用,很有借鉴性

一、项目结构

            

           主要由一个布局文件activity_title.xml和一个构造类TitleBuilder.java组成,布局文件定义标题栏样式,构造类负责设定文字内容,是否显示返回按钮等等。

二、代码设计 

activity_title.xml 由一个绝对布局包裹两个图片控件和一个文本框控件组成

此处使用线性布局嵌套绝对布局,使内容可以更好地居中显示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#1B77A8"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="50dp">
<RelativeLayout
android:id="@+id/title_bar"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_centerVertical="true"
android:visibility="gone"
android:id="@+id/title_leftIco"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/title_text"
android:textSize="20sp"
android:textColor="#ffffff"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:visibility="gone"
android:id="@+id/title_rightIco"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>

TitleBuilder.java

    /**
* Created by PanZhe on 2016/7/22.
*/
public class TitleBuilder {
private View titleView;
private RelativeLayout titleBar;
private TextView text;
private ImageView leftIco;
private ImageView rightIco; /**
* 构造方法:用于获取对象
* */
public TitleBuilder(Activity context){
titleView = context.findViewById(R.id.title_bar);
text = (TextView)titleView.findViewById(R.id.title_text);
titleBar = (RelativeLayout)titleView.findViewById(R.id.title_bar);
leftIco = (ImageView)titleView.findViewById(R.id.title_leftIco);
rightIco = (ImageView)titleView.findViewById(R.id.title_rightIco);
} /**
* 用于设置标题栏文字
* */
public TitleBuilder setTitleText(String titleText){
if(!TextUtils.isEmpty(titleText)){
text.setText(titleText);
}
return this;
} /**
* 用于设置标题栏左边要显示的图片
* */
public TitleBuilder setLeftIco(int resId){
leftIco.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);
leftIco.setImageResource(resId);
return this;
} /**
* 用于设置标题栏右边要显示的图片
* */
public TitleBuilder setRightIco(int resId){
rightIco.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);
rightIco.setImageResource(resId);
return this;
} /**
* 用于设置标题栏左边图片的单击事件
* */
public TitleBuilder setLeftIcoListening(View.OnClickListener listener){
if(leftIco.getVisibility() == View.VISIBLE){
leftIco.setOnClickListener(listener);
}
return this;
} /**
* 用于设置标题栏右边图片的单击事件
* */
public TitleBuilder setRightIcoListening(View.OnClickListener listener){
if(rightIco.getVisibility() == View.VISIBLE){
rightIco.setOnClickListener(listener);
}
return this;
}
}

三、使用方法

   在需要的地方引入

    <include layout="@layout/activity_title"/>  

在java代码中使用new方法循环设置参数

    public class MainActivity extends AppCompatActivity {  

        @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); new TitleBuilder(this).setTitleText("测试标题").setLeftIco(R.drawable.left).setLeftIcoListening(leftReturnListener);
} private View.OnClickListener leftReturnListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
};
}

四、效果图

中规中矩,对不对?
大家可以通过修改TitleBuild构造类中的方法来实现自己满意的效果,在这里就不一一列举了

(转)Android开发之封装标题栏的更多相关文章

  1. Android开发-取消程序标题栏或自定义标题栏

    注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 在Android开发中,跟据需要我们有时候需要自定义应用程序的标题栏或者取消程序的标题栏,下面本菜鸟在此记录与分享一下自己使用的方法 ...

  2. Android开发如何去除标题栏title(转)

    去除标题栏title其实非常简单,他有两种方法,一种是在代码中添加,另一种是在AndroidManifest.xml中添加: 1.在代码中实现:在此方法setContentView(R.layout. ...

  3. Android开发如何去除标题栏title

    虽然是一个小问题,今天遇到了,也就写下来吧.防止自己忘掉. 取消标题栏的方式有两种,一种是在代码添加,另一种是在AndroidManifest.xml里面添加. 1.在代码中实现:在此方法setCon ...

  4. Android开发之去掉标题栏的三种方法,推荐第三种

    Android:去掉标题栏的三种方法和全屏的三种方法 第一种:一般入门的时候常常使用的一种方法 onCreate函数中增加下面代码: requestWindowFeature(Window.FEATU ...

  5. Android开发之封装log打印日志的工具类,实用logutils详细代码

    public final class LogUtil { /** all Log print on-off */ private final static boolean all = true; /* ...

  6. android 开发 - 使用okhttp框架封装的开发框架

    概述 在android开发中经常要访问网络,目前最流行的网络访问框架就是Okhttp了,然而我们在具体使用时,往往仍然需要二次封装.我使用Builder设计模式进行了封装形成oknet开源库. 介绍 ...

  7. Android开发--去掉标题栏

    Android开发中为了尽可能美观,会去掉标题栏.去掉标题栏有三种方法. 一.在Activity代码里实现 在代码中实现以下方法: this.requestWindowFeature(Window.F ...

  8. android 开发 使用自定义布局实现标题栏复用(标题栏内容自定义:使用代码实现和xml布局自定义属性2种办法实现)

    在个人学习的情况下可能很少使用自定义布局去实现大量复用的情况下,但是在一个开发工作的环境下就会使用到大量复用的自定义控件. 实现思维: 1.写一个xml的布局,用于标题栏的样式,并且添加在标题栏中你想 ...

  9. 史上最全系列Android开发环境搭建

    一.安装JDK1.JDK下载打开网站http://www.oracle.com/technetwor ... nloads-1880260.html,选择相应的操作系统下载JDK 2.安装JDK本机是 ...

随机推荐

  1. unity3d热更新插件uLua学习整理

    前言 IOS不能热更新,不是因为不能用反射,是因为System.Reflection.Assembly.Load 无法使用System.Reflection.Emit 无法使用System.CodeD ...

  2. MongoDB模糊查询,以及MongoDB模糊查询时带有括号的情况

    模糊查询 记录如下: { "_id" : ObjectId("5c3d486d24aa9a000526367b"), "name" : &q ...

  3. 本地Navicat连不上Linux虚拟机MySQL数据库问题

    LinuxAndMySQL 版权声明:本文为博主原创文章,未经博主允许不得转载. 在Linux登录到MySQL数据:mysql -uroot -p 输入密码 切换到mysql数据库 mysql> ...

  4. centos7升级内核

    载入elrepo源,搜索内核更新资源,并进行更新操作. 具体实验步骤: # 载入公钥rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org ...

  5. Flux architecture

    [Flux architecture] Flux is a pattern for managing data flow in your application. The most important ...

  6. elasticsearch 不同集群数据同步

    采用快照方式 1.源集群采用NFS,注意权限 2.共享目录完成后,在所有ES服务器上挂载为同一目录 3.创建快照仓库 put _snapshot/my_backup{ "type" ...

  7. matlab读取excel里的数据并用imagesc画图

    把矩阵数据保存在excel里 比如文件为 a.xlsx 通过下面的程序读取 a=xlsread('\文件保存的目录\a.xlsx'); figure(1); imagesc(a) colormap(h ...

  8. pta7-20 畅通工程之局部最小花费问题(Kruskal算法)

    题目链接:https://pintia.cn/problem-sets/15/problems/897 题意:给出n个城镇,然后给出n×(n-1)/2条边,即每两个城镇之间的边,包含起始点,终点,修建 ...

  9. Array 遍历数组

    public static void main(String args){ int a[][] = new int[3][4]; for(int i=0;i<a.length;i++){ for ...

  10. nexus(Maven仓库私服)的安装、配置、使用和仓库迁移

    简介 Nexus下载:点击进入 Nexus 是Maven仓库管理器,如果你使用Maven,你可以从Maven中央仓库 下载所需要的构件(artifact),但这通常不是一个好的做法,你应该在本地架设一 ...