不知不觉标题自定义标题栏这个功能已经写了有将近半年的时间了,总共花了一个来月的下班时间才将其完成,完全靠自己的开发的确有点难,但,说实在的也不难,当初觉得难是因为自己对自定义的View点击事件一窍不通,翻了许多博客,迟迟未找到处理方法,直到后面脑子换了种思路去思考这个问题,才将其处理掉。

自定义标题栏,就要隐藏掉自带的标题栏,隐藏标题栏只需要在AndroidManifest.xml文件的application标签theme的引用样式的父样式更换为有NoActionBar字样的即可


标题栏隐藏了,好,我们正式开始进入

新建一个class继承一个相对布局并重写其构造方法

public class CustomTitleBlock extends RelativeLayout {public CustomTitleBlock(Context context) {super(context);}public CustomTitleBlock(Context context, AttributeSet attrs) {super(context, attrs);}public CustomTitleBlock(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public CustomTitleBlock(Context context, AttributeSet attrs, int defStyleAttr,int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);}
}

创建一个Layout文件用于CustomTitleBlock.class绑定并初始化相关控件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/layout_title"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="12dp"android:layout_marginRight="12dp"><androidx.constraintlayout.widget.ConstraintLayoutandroid:id="@+id/left"android:layout_width="wrap_content"android:layout_height="match_parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent"><ImageViewandroid:id="@+id/iv_left"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/tv_left"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toRightOf="@+id/iv_left"app:layout_constraintTop_toTopOf="parent"  /></androidx.constraintlayout.widget.ConstraintLayout><TextViewandroid:id="@+id/tv_centent"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textStyle="bold"android:textSize="16sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintLeft_toLeftOf="parent"/><EditTextandroid:id="@+id/search_box"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginTop="4dp"android:layout_marginBottom="4dp"android:textSize="14sp"android:background="@drawable/search_box_style"android:drawableLeft="@drawable/search_64"android:drawablePadding="6dp"android:paddingLeft="8dp"android:paddingRight="8dp"android:visibility="invisible"app:layout_constraintLeft_toRightOf="@+id/left"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintRight_toLeftOf="@+id/right"/><androidx.constraintlayout.widget.ConstraintLayoutandroid:id="@+id/right"android:layout_width="wrap_content"android:layout_height="match_parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintRight_toRightOf="parent"><TextViewandroid:id="@+id/tv_right"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintRight_toLeftOf="@+id/iv_right"/><ImageViewandroid:id="@+id/iv_right"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent"/></androidx.constraintlayout.widget.ConstraintLayout></androidx.constraintlayout.widget.ConstraintLayout></RelativeLayout>

在values文件夹下创建一个attrs.xml文件用于声明控件的属性

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="CustomTitleBlock"><!-- 标题栏左边 --><attr name="leftText" format="string"/><attr name="leftTextColor" format="color"/><attr name="leftTextSize" format="integer"/><attr name="leftImage" format="reference"/><!-- 标题栏中间 --><attr name="centerText" format="string"/><attr name="centerTextColor" format="color"/><attr name="centerTextSize" format="integer"/><!-- 搜索框 --><attr name="searchBox" format="boolean"/><attr name="searchBoxHint" format="string"/><attr name="searchBoxWidth" format="dimension"><enum name="match_parent" value="-1"/><enum name="wrap_content" value="-2"/></attr><!-- 标题栏右边 --><attr name="rightText" format="string"/><attr name="rightTextColor" format="color"/><attr name="rightTextSize" format="integer"/><attr name="rightImage" format="reference"/></declare-styleable>
</resources>

接下来我们可以通过Context.obtainStyledAttributes方法绑定attrs文件的declare-styleable属性,返回的TypedArray可以获得declare-styleable属性的索引

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleBlock, defStyleAttr, 0);

因为我设置的declare-styleable属性不止一条,需要使用循环来进行遍历,通过遍历来获得相对应的属性

for (int i = 0; i < count; i++) {int attr = typedArray.getIndex(i);switch (attr) {case R.styleable.CustomTitleBlock_leftText:// 此处写该属性赋值后的逻辑应该进行什么样的操作break;case R.styleable.CustomTitleBlock_leftTextColor:// 此处写该属性赋值后的逻辑应该进行什么样的操作break;}
}
// 循环完要记得回收
typedArray.recycle();

其中,R.styleable.CustomTitleBlock_leftText 中,下划线前面指的是在values目录下创建的attrs文件里面的 declare-styleable ,下划线后面指的是该 declare-styleable 下的 attr
还需要在layout文件引用就可以看见效果啦

<com.demo.maininterface.view.CustomTitleBlockandroid:layout_width="match_parent"android:layout_height="46dp"app:leftImage="@drawable/return_left"app:centerText="@string/bluetooth"/>


光有效果还不行,触发事件还没有,无疑是摆设,自定义的控件点击事件并不难,声明几个公共方法去实现即可

    public void setOnLeftClickListener(OnClickListener onClickListener){left.setOnClickListener(onClickListener);}public void setOnRightClickListener(OnClickListener onClickListener){right.setOnClickListener(onClickListener);}

至此,自定义标题栏已完成,有不懂的可以下载文件观看哦-点击下载本文代码

【Android】实现自定义标题栏相关推荐

  1. Android 安卓 自定义标题栏+沉浸式状态栏

    Android 安卓 自定义标题栏+沉浸式状态栏 由于Android原生的标题栏单调,简单,并不是很好看,自定义个性化的标题栏可以让app更加美观. Android 4.4以后增加了沉浸式透明状态栏. ...

  2. Android:自定义标题栏

    现在很多的Android程序都在标题栏上都显示了一些按钮和标题,这里尝试做个实例 在onCreate中添加: //自定义标题 requestWindowFeature(Window.FEATURE_C ...

  3. Android之自定义标题栏

    引入布局 在layout目录下新建一个title.xml布局,加入两个Button和一个TextView. 应用这个标题栏需要去相应xml布局文件中用include语句引入. 还需要把系统自带的标题栏 ...

  4. 安卓学习随笔 -- 自定义标题栏

    在安卓中不喜欢系统默认的标题栏,那么如何让自定义一个自己的标题栏呢. 自定义后的标题栏如下: 首先这里需要定义一个自定义的标题栏布局 title.xml文件 (里边需要两个图片这个很简单) <R ...

  5. java怎么设置窗体标题_Android窗体自定义标题栏

    自定义实现功能图片如下: ? ? ? package com.easyway.titlebar; import android.app.Activity; import android.os.Bund ...

  6. Andorid 自定义标题栏

    效果如图: 先在strings.xml 中定义主题 <style name="customTitlebg" ><item name="android:b ...

  7. android分享的主标题,Android 自定义标题栏(title栏)

    近日 需要在android的标题栏上添加按钮,所以对android的标题栏进行了一下简单的研究- 第一步,向实现自定义标题栏,需要在onCreate方法里这样写 requestWindowFeatur ...

  8. android 标题栏进度圈使用方法,Android 自定义标题栏 显示网页加载进度的方法实例...

    这阵子在做Lephone的适配,测试组提交一个bug:标题栏的文字较长时没有显示完全,其实这并不能算个bug,并且这个问题在以前其他机器也没有出现,只是说在Lephone的这个平台上显示得不怎么美观, ...

  9. Android学习之自定义标题栏

    一.如今好多的Android APP都在界面的顶部放置一个标题栏,标题栏里通常会有一两个按钮可用于返回和其它操作,虽然Android系统中已经给每个Activity提供了标题栏功能,但是这里介绍一下我 ...

  10. android标题栏上增加按钮,Android:向自定义标题栏添加按钮

    我创建了一个自定义标题栏,如本例所示 "一个自定义标题栏" – 一半下来. 在某些活动中,我想在标题栏的右侧放置一个按钮(与facebook app相同).我试图按如下方式向视图添 ...

最新文章

  1. Alibaba代码规范插件、FindBugs插件安装及详解,IDEA插件安装,代码规范,代码查错,代码格式规范
  2. mysql int(40)_MySQL Integer类型与INT(11)
  3. springboot 使用interceptor 返回前端http状态码为0
  4. Asp.net几大内置对象
  5. python学习-条件语句
  6. 日本字全角字符linux,Linux命令行输入全角字符被转化
  7. Unable to open log device '/dev/log/main' : No such file or directory it ...
  8. 麻省理工18年春软件构造课程阅读07“设计规格说明”
  9. vi/vim替换字符
  10. 详述白盒测试的逻辑覆盖法的条件组合覆盖及其优缺点
  11. 一元线性回归(R语言)
  12. oracle获取中位数
  13. 如何将一个向量投影到一个平面上_数学一轮复习32,平面向量数量积及其应用,三角形‘四心’模型...
  14. WaWa的奇妙冒险(第二周集训自闭现场)
  15. mongo-节点出现recovering状态的处理办法
  16. LaTeX 中使用三级标题
  17. Android 程序员必须掌握的三种自动化测试方法
  18. ZDNS .网址注册局发布2020年第二季度全球域名发展统计报告
  19. Linux服务器常用命令 - 记录(Anaconda/Matlab/VNC/Python)
  20. BLUEMOON记录

热门文章

  1. 算力测试Linux,附录:计算力的标准Linpack测试详细指南(1)
  2. 绿色全要素生产率数据(2004-2017年)
  3. Bingo学习--jdk1.8新特性
  4. 阿里云oss出现Unable to execute HTTP request: bucket.二级域名.域名: nodename nor servname provided, or not known
  5. Glide源码分析以及三级缓存原理
  6. 数字排列问题(全排例)
  7. 「首席架构师推荐」数值分析软件列表
  8. 3d max材质贴图
  9. python中终结一个循环的保留字_【单选题】以下可以终结一个循环的保留字是() (2.0分) A. if B. break C. exit D. continue...
  10. 从PowerDesigner概念设计模型(CDM)中的3种实体关系说起