【Android自定义View实战】之自定义项目通用的标题栏CustomTitleBar

本文主要是介绍【Android自定义View实战】之自定义项目通用的标题栏CustomTitleBar,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/52948152 【DylanAndroid的csdn博客】


在Android开发中,一般来说项目中都会用到一个通用风格的标题栏,比如说左边返回按钮,中间显示标题,最后边可能会有一个保存按钮。如下图
这里写图片描述
那么我们是不是每次在新建一个布局的时候都要去用一个线性布局去加载三个控件,特别麻烦。我们自定义之后,一个控件就好了,下面就来看一下如何来打造通用的自定义标题栏。

1.首先在layout文件夹下定义一个pub_titlebar.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/ll"android:layout_width="match_parent"android:layout_height="48dp"android:background="#654454"android:orientation="horizontal"><TextView
        android:id="@+id/left_button"android:layout_width="48dp"android:layout_height="match_parent"android:textColor="#FFFFFF"android:background="@mipmap/arrow_left"/><TextView
        android:id="@+id/title"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:textColor="#000000"android:text="标题"android:textSize="20sp" /><TextView
        android:id="@+id/right_button"android:layout_width="48dp"android:layout_height="match_parent"android:background="@null"android:gravity="center"android:textColor="#000000"android:text="保存"android:textSize="16sp" />
</LinearLayout>

2.在res的values文件夹下新建一个attrs.xml

这些可以设置的变量可以在布局文件中动态去设置,特别方便

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="CustomTitleBar"><attr name="title_background" format="color"/><attr name="left_button_image" format="reference|integer"/><attr name="left_button_text" format="string"/><attr name="left_button_textColor" format="color"/><attr name="left_button_textSize" format="dimension"/><attr name="show_left_button" format="boolean"/><attr name="title_text" format="string"/><attr name="title_textColor" format="color"/><attr name="title_textSize" format="dimension"/><attr name="right_button_image" format="reference|integer"/><attr name="right_button_text" format="string"/><attr name="right_button_textColor" format="color"/><attr name="right_button_textSize" format="dimension"/><attr name="show_right_button" format="boolean"/></declare-styleable>
</resources>

3.自定义CustomTitleBar继承自LinearLayout


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;/*** Android自定义标题栏* Created by yuandl on 2016-10-27.*/public class CustomTitleBar extends LinearLayout {/*** 标题栏的根布局*/private LinearLayout ll;/*** 标题栏的左边按返回按钮*/private TextView left_button;/*** 标题栏的右边按保存按钮*/private TextView right_button;/*** 标题栏的中间的文字*/private TextView title;/*** 标题栏的背景颜色*/private int title_background_color;/*** 标题栏的显示的标题文字*/private String title_text;/*** 标题栏的显示的标题文字颜色*/private int title_textColor;/*** 标题栏的显示的标题文字大小*/private int title_textSize;/*** 返回按钮的资源图片*/private int left_button_imageId;/*** 返回按钮上显示的文字*/private String left_button_text;/*** 返回按钮上显示的文字颜色*/private int left_button_textColor;/*** 返回按钮上显示的文字大小*/private int left_button_textSize;/*** 是否显示返回按钮*/private boolean show_left_button;/*** 右边保存按钮的资源图片*/private int right_button_imageId;/*** 右边保存按钮的文字*/private String right_button_text;/*** 右边保存按钮的文字颜色*/private int right_button_textColor;/*** 右边保存按钮的文字大小*/private int right_button_textSize;/*** 是否显示右边保存按钮*/private boolean show_right_button;/*** 标题的点击事件*/private TitleOnClickListener titleOnClickListener;public CustomTitleBar(Context context, AttributeSet attrs) {super(context, attrs);/**加载布局文件*/LayoutInflater.from(context).inflate(R.layout.pub_titlebar, this, true);ll = (LinearLayout) findViewById(R.id.ll);left_button = (TextView) findViewById(R.id.left_button);right_button = (TextView) findViewById(R.id.right_button);title = (TextView) findViewById(R.id.title);/**获取属性值*/TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleBar);/**标题相关*/title_background_color = typedArray.getColor(R.styleable.CustomTitleBar_title_background, Color.WHITE);title_text = typedArray.getString(R.styleable.CustomTitleBar_title_text);title_textColor = typedArray.getColor(R.styleable.CustomTitleBar_title_textColor, Color.BLACK);title_textSize = typedArray.getColor(R.styleable.CustomTitleBar_title_textSize, 22);/**返回按钮相关*/left_button_imageId = typedArray.getResourceId(R.styleable.CustomTitleBar_left_button_image, R.mipmap.arrow_left);left_button_text = typedArray.getString(R.styleable.CustomTitleBar_left_button_text);left_button_textColor = typedArray.getColor(R.styleable.CustomTitleBar_left_button_textColor, Color.WHITE);left_button_textSize = typedArray.getColor(R.styleable.CustomTitleBar_left_button_textSize, 20);show_left_button = typedArray.getBoolean(R.styleable.CustomTitleBar_show_left_button, true);/**右边保存按钮相关*/right_button_imageId = typedArray.getResourceId(R.styleable.CustomTitleBar_right_button_image, 0);right_button_text = typedArray.getString(R.styleable.CustomTitleBar_right_button_text);right_button_textColor = typedArray.getColor(R.styleable.CustomTitleBar_right_button_textColor, Color.WHITE);right_button_textSize = typedArray.getColor(R.styleable.CustomTitleBar_right_button_textSize, 20);show_right_button = typedArray.getBoolean(R.styleable.CustomTitleBar_show_right_button, true);/**设置值*/setTitle_background_color(title_background_color);setTitle_text(title_text);setTitle_textSize(title_textSize);setTitle_textColor(title_textColor);setShow_left_button(show_left_button);setShow_right_button(show_right_button);if (!TextUtils.isEmpty(left_button_text)) {//返回按钮显示为文字setLeft_button_text(left_button_text);setLeft_button_textColor(left_button_textColor);setLeft_button_textSize(left_button_textSize);} else {setLeft_button_imageId(left_button_imageId);}if (!TextUtils.isEmpty(right_button_text)) {//返回按钮显示为文字setRight_button_text(right_button_text);setRight_button_textColor(right_button_textColor);setRight_button_textSize(right_button_textSize);} else {setRight_button_imageId(right_button_imageId);}left_button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (titleOnClickListener != null) {titleOnClickListener.onLeftClick();}}});right_button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (titleOnClickListener != null) {titleOnClickListener.onRightClick();}}});}/*** 获取左边的返回按钮** @return Button*/public TextView getLeft_button() {return left_button;}/*** 获取标题栏的跟布局** @return LinearLayout*/public LinearLayout getLl() {return ll;}/*** 获取标题栏标题TextView** @return TextView*/public TextView getTitle() {return title;}/*** 获取右边的保存按钮** @return Button*/public TextView getRight_button() {return right_button;}/*** 设置返回按钮的资源图片id** @param left_button_imageId 资源图片id*/public void setLeft_button_imageId(int left_button_imageId) {left_button.setBackgroundResource(left_button_imageId);}/*** 设置返回按钮的文字** @param left_button_text*/public void setLeft_button_text(String left_button_text) {left_button.setText(left_button_text);}/*** 设置返回按钮的文字颜色** @param left_button_textColor*/public void setLeft_button_textColor(int left_button_textColor) {left_button.setTextColor(left_button_textColor);}/*** 设置返回按钮的文字大小** @param left_button_textSize*/public void setLeft_button_textSize(int left_button_textSize) {left_button.setTextSize(left_button_textSize);}/*** 设置是否显示返回按钮** @param show_left_button*/public void setShow_left_button(boolean show_left_button) {left_button.setVisibility(show_left_button ? VISIBLE : INVISIBLE);}/*** 设置右边保存按钮的资源图片** @param right_button_imageId*/public void setRight_button_imageId(int right_button_imageId) {right_button.setBackgroundResource(right_button_imageId);}/*** 设置右边的保存按钮的文字** @param right_button_text*/public void setRight_button_text(String right_button_text) {right_button.setText(right_button_text);}/*** 设置右边保存按钮的文字颜色** @param right_button_textColor*/public void setRight_button_textColor(int right_button_textColor) {right_button.setTextColor(right_button_textColor);}/*** 设置右边保存按钮的文字大小** @param right_button_textSize*/public void setRight_button_textSize(int right_button_textSize) {right_button.setTextSize(right_button_textSize);}/*** 设置是显示右边保存按钮** @param show_right_button*/public void setShow_right_button(boolean show_right_button) {right_button.setVisibility(show_right_button ? VISIBLE : INVISIBLE);}/*** 设置标题背景的颜色** @param title_background_color*/public void setTitle_background_color(int title_background_color) {ll.setBackgroundColor(title_background_color);}/*** 设置标题的文字** @param title_text*/public void setTitle_text(String title_text) {title.setText(title_text);}/*** 设置标题的文字颜色** @param title_textColor*/public void setTitle_textColor(int title_textColor) {title.setTextColor(title_textColor);}/*** 设置标题的文字大小** @param title_textSize*/public void setTitle_textSize(int title_textSize) {title.setTextSize(title_textSize);}/*** 设置标题的点击监听** @param titleOnClickListener*/public void setOnTitleClickListener(TitleOnClickListener titleOnClickListener) {this.titleOnClickListener = titleOnClickListener;}/*** 监听标题点击接口*/public interface TitleOnClickListener {/*** 返回按钮的点击事件*/void onLeftClick();/*** 保存按钮的点击事件*/void onRightClick();}
}

4.用法

  • 在布局文件中的引用

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="cn.bluemobi.dylan.MainActivity"><cn.bluemobi.dylan.CustomTitleBar
        android:id="@+id/ct"android:layout_width="match_parent"android:layout_height="wrap_content"app:title_text="标题一"></cn.bluemobi.dylan.CustomTitleBar><cn.bluemobi.dylan.CustomTitleBar
        android:layout_marginTop="10dp"android:layout_width="match_parent"android:layout_height="wrap_content"app:show_left_button="false"app:title_text="标题二"></cn.bluemobi.dylan.CustomTitleBar><cn.bluemobi.dylan.CustomTitleBar
        android:layout_marginTop="10dp"android:layout_width="match_parent"android:layout_height="wrap_content"app:show_right_button="false"app:title_text="标题三"></cn.bluemobi.dylan.CustomTitleBar><cn.bluemobi.dylan.CustomTitleBar
        android:layout_marginTop="10dp"android:layout_width="match_parent"android:layout_height="wrap_content"app:show_left_button="false"app:show_right_button="false"app:title_text="标题四"></cn.bluemobi.dylan.CustomTitleBar>
    </LinearLayout>
    
  • 在Activity中的用法

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);CustomTitleBar customTitleBar = (CustomTitleBar) findViewById(R.id.ct);customTitleBar.setOnTitleClickListener(new CustomTitleBar.TitleOnClickListener() {@Overridepublic void onLeftClick() {Toast.makeText(MainActivity.this, "点击了左边的返回按钮", Toast.LENGTH_SHORT).show();}@Overridepublic void onRightClick() {Toast.makeText(MainActivity.this, "点击了右边的保存按钮", Toast.LENGTH_SHORT).show();}});}
}

5.效果图

这里写图片描述

6 GitHub地址

这篇关于【Android自定义View实战】之自定义项目通用的标题栏CustomTitleBar的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/996005

相关文章

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

PowerShell中15个提升运维效率关键命令实战指南

《PowerShell中15个提升运维效率关键命令实战指南》作为网络安全专业人员的必备技能,PowerShell在系统管理、日志分析、威胁检测和自动化响应方面展现出强大能力,下面我们就来看看15个提升... 目录一、PowerShell在网络安全中的战略价值二、网络安全关键场景命令实战1. 系统安全基线核查

从原理到实战深入理解Java 断言assert

《从原理到实战深入理解Java断言assert》本文深入解析Java断言机制,涵盖语法、工作原理、启用方式及与异常的区别,推荐用于开发阶段的条件检查与状态验证,并强调生产环境应使用参数验证工具类替代... 目录深入理解 Java 断言(assert):从原理到实战引言:为什么需要断言?一、断言基础1.1 语