【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

相关文章

vite搭建vue3项目的搭建步骤

《vite搭建vue3项目的搭建步骤》本文主要介绍了vite搭建vue3项目的搭建步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1.确保Nodejs环境2.使用vite-cli工具3.进入项目安装依赖1.确保Nodejs环境

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

idea+spring boot创建项目的搭建全过程

《idea+springboot创建项目的搭建全过程》SpringBoot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目,:本文主要介绍idea+springb... 目录一.idea四种搭建方式1.Javaidea命名规范2JavaWebTomcat的安装一.明确tomcat

pycharm跑python项目易出错的问题总结

《pycharm跑python项目易出错的问题总结》:本文主要介绍pycharm跑python项目易出错问题的相关资料,当你在PyCharm中运行Python程序时遇到报错,可以按照以下步骤进行排... 1. 一定不要在pycharm终端里面创建环境安装别人的项目子模块等,有可能出现的问题就是你不报错都安装

Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题

《Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题》在爬虫工程里,“HTTPS”是绕不开的话题,HTTPS为传输加密提供保护,同时也给爬虫带来证书校验、... 目录一、核心问题与优先级检查(先问三件事)二、基础示例:requests 与证书处理三、高并发选型:

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

MyCat分库分表的项目实践

《MyCat分库分表的项目实践》分库分表解决大数据量和高并发性能瓶颈,MyCat作为中间件支持分片、读写分离与事务处理,本文就来介绍一下MyCat分库分表的实践,感兴趣的可以了解一下... 目录一、为什么要分库分表?二、分库分表的常见方案三、MyCat简介四、MyCat分库分表深度解析1. 架构原理2. 分

Oracle Scheduler任务故障诊断方法实战指南

《OracleScheduler任务故障诊断方法实战指南》Oracle数据库作为企业级应用中最常用的关系型数据库管理系统之一,偶尔会遇到各种故障和问题,:本文主要介绍OracleSchedul... 目录前言一、故障场景:当定时任务突然“消失”二、基础环境诊断:搭建“全局视角”1. 数据库实例与PDB状态2

Git进行版本控制的实战指南

《Git进行版本控制的实战指南》Git是一种分布式版本控制系统,广泛应用于软件开发中,它可以记录和管理项目的历史修改,并支持多人协作开发,通过Git,开发者可以轻松地跟踪代码变更、合并分支、回退版本等... 目录一、Git核心概念解析二、环境搭建与配置1. 安装Git(Windows示例)2. 基础配置(必