Android 组合控件实现布局的复用的方法
发布时间 - 2026-01-11 02:35:57 点击率:次看到很多项目会有实现自己的标题栏的做法,通常的界面是左边按钮或文字,加上中间的标题和右边的按钮或文字组成的。比较好的一种做法是使用include标签,复用同一个xml文件来实现布局的复用。但是这种方法是通过代码的方式来设置标题,左右按钮等其他的属性,会导致布局属性和Activity代码耦合性比较高。

因此,我们要通过自定义View,继承ViewGroup子类来实现这样的布局,降低布局文件和Activity代码耦合性。
首先,我们需要写出布局文件layout_custom_titlebar.xml。
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 使用merge标签减少层级 --> <Button android:id="@+id/title_bar_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:background="@null" android:minHeight="45dp" android:minWidth="45dp" android:textSize="14sp" /> <TextView android:id="@+id/title_bar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:singleLine="true" android:textSize="17sp" /> <Button android:id="@+id/title_bar_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="7dp" android:background="@null" android:minHeight="45dp" android:minWidth="45dp" android:textSize="14sp" /> </merge>
2.定义自定义属性
<declare-styleable name="CustomTitleBar"> <!--标题栏背景色--> <attr name="title_background_color" format="reference|integer" /> <!--左边按钮是否可见--> <attr name="left_button_visible" format="boolean" /> <!--右边按钮是否可见--> <attr name="right_button_visible" format="boolean" /> <!--标题文字--> <attr name="title_text" format="string" /> <!--标题文字颜色--> <attr name="title_text_color" format="color" /> <!--标题文字图标--> <attr name="title_text_drawable" format="reference|integer" /> <!--左边按钮文字--> <attr name="left_button_text" format="string" /> <!--左边按钮文字颜色--> <attr name="left_button_text_color" format="color" /> <!--左边按钮图标--> <attr name="left_button_drawable" format="reference|integer" /> <!--右边按钮文字--> <attr name="right_button_text" format="string" /> <!--右边按钮文字颜色--> <attr name="right_button_text_color" format="color" /> <!--右边按钮图标--> <attr name="right_button_drawable" format="reference|integer" /> </declare-styleable>
3.自定义一个View继承ViewGroup子类,这里我们继承RelativeLayout。
public class CustomTitleBar extends RelativeLayout {
private Button titleBarLeftBtn;
private Button titleBarRightBtn;
private TextView titleBarTitle;
public CustomTitleBar(Context context) {
super(context);
}
public CustomTitleBar(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.layout_custom_titlebar,this,true);
titleBarLeftBtn = (Button) findViewById(R.id.title_bar_left);
titleBarRightBtn = (Button) findViewById(R.id.title_bar_right);
titleBarTitle = (TextView) findViewById(R.id.title_bar_title);
TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.CustomTitleBar);
if(typedArray!=null){
//titleBar背景色
int titleBarBackGround=typedArray.getResourceId(R.styleable.CustomTitleBar_title_background_color, Color.BLUE);
setBackgroundColor(titleBarBackGround);
//获取是否要显示左边按钮
boolean leftButtonVisible = typedArray.getBoolean(R.styleable.CustomTitleBar_left_button_visible, true);
if (leftButtonVisible) {
titleBarLeftBtn.setVisibility(View.VISIBLE);
} else {
titleBarLeftBtn.setVisibility(View.INVISIBLE);
}
//设置左边按钮的文字
String leftButtonText = typedArray.getString(R.styleable.CustomTitleBar_left_button_text);
if (!TextUtils.isEmpty(leftButtonText)) {
titleBarLeftBtn.setText(leftButtonText);
//设置左边按钮文字颜色
int leftButtonTextColor = typedArray.getColor(R.styleable.CustomTitleBar_left_button_text_color, Color.WHITE);
titleBarLeftBtn.setTextColor(leftButtonTextColor);
} else {
//设置左边图片icon 这里是二选一 要么只能是文字 要么只能是图片
int leftButtonDrawable = typedArray.getResourceId(R.styleable.CustomTitleBar_left_button_drawable, R.mipmap.titlebar_back_icon);
if (leftButtonDrawable != -1) {
titleBarLeftBtn.setBackgroundResource(leftButtonDrawable);
}
}
//先获取标题是否要显示图片icon
int titleTextDrawable = typedArray.getResourceId(R.styleable.CustomTitleBar_title_text_drawable, -1);
if (titleTextDrawable != -1) {
titleBarTitle.setBackgroundResource(titleTextDrawable);
} else {
//如果不是图片标题 则获取文字标题
String titleText = typedArray.getString(R.styleable.CustomTitleBar_title_text);
if (!TextUtils.isEmpty(titleText)) {
titleBarTitle.setText(titleText);
}
//获取标题显示颜色
int titleTextColor = typedArray.getColor(R.styleable.CustomTitleBar_title_text_color, Color.WHITE);
titleBarTitle.setTextColor(titleTextColor);
}
//获取是否要显示右边按钮
boolean rightButtonVisible = typedArray.getBoolean(R.styleable.CustomTitleBar_right_button_visible, true);
if (rightButtonVisible) {
titleBarRightBtn.setVisibility(View.VISIBLE);
} else {
titleBarRightBtn.setVisibility(View.INVISIBLE);
}
//设置右边按钮的文字
String rightButtonText = typedArray.getString(R.styleable.CustomTitleBar_right_button_text);
if (!TextUtils.isEmpty(rightButtonText)) {
titleBarRightBtn.setText(rightButtonText);
//设置右边按钮文字颜色
int rightButtonTextColor = typedArray.getColor(R.styleable.CustomTitleBar_right_button_text_color, Color.BLUE);
titleBarRightBtn.setTextColor(rightButtonTextColor);
} else {
//设置右边图片icon 这里是二选一 要么只能是文字 要么只能是图片
int rightButtonDrawable = typedArray.getResourceId(R.styleable.CustomTitleBar_right_button_drawable, -1);
if (rightButtonDrawable != -1) {
titleBarRightBtn.setBackgroundResource(rightButtonDrawable);
}
}
typedArray.recycle();
}
}
public void setTitleClickListener(OnClickListener onClickListener) {
if (onClickListener != null) {
titleBarLeftBtn.setOnClickListener(onClickListener);
titleBarRightBtn.setOnClickListener(onClickListener);
}
}
public Button getTitleBarLeftBtn() {
return titleBarLeftBtn;
}
public Button getTitleBarRightBtn() {
return titleBarRightBtn;
}
public TextView getTitleBarTitle() {
return titleBarTitle;
}
}
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" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.mumubin.demoproject.view.CustomTitleBar android:id="@+id/ctb_view" android:layout_width="match_parent" android:layout_height="45dp" app:right_button_drawable="@mipmap/sure" app:title_text="@string/app_name" /> <com.mumubin.demoproject.view.CustomTitleBar android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginTop="4dp" app:title_background_color="@color/colorPrimary" app:title_text="@string/app_name" app:title_text_color="@color/colorAccent" app:left_button_text="左边" app:right_button_text="右边"/> <com.mumubin.demoproject.view.CustomTitleBar android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginTop="4dp" app:title_text_drawable="@mipmap/ic_launcher" app:title_background_color="@color/colorAccent" app:left_button_text="左边" app:right_button_text="右边"/> </LinearLayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# Android
# 布局复用
# 布局的复用
# Android动态添加设置布局与控件的方法
# Android开发之基本控件和四种布局方式详解
# Android 布局控件之LinearLayout详细介绍
# Android 图片网格布局控件示例代码
# Android布局控件之常用linearlayout布局
# Android布局优化之ViewStub控件
# Android编程布局控件之AbsoluteLayout用法实例分析
# Android 仿京东商城底部布局的选择效果(Selector 选择器的实现)
# Android时间选择器、日期选择器实现代码
# Android开发实现布局中为控件添加选择器的方法
# 自定义
# 子类
# 来实现
# 自己的
# 背景色
# 复用
# 标题栏
# 会有
# 其他的
# 较高
# 比较好
# 如果不是
# 大家多多
# 正确地
# 这种方法
# 使用它
# singleLine
# layout_centerInParent
# title_bar_title
# declare
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
免费网站制作appp,免费制作app哪个平台好?
作用域操作符会触发自动加载吗_php类自动加载机制与::调用【教程】
Laravel如何生成PDF或Excel文件_Laravel文档导出工具与使用教程
武汉网站设计制作公司,武汉有哪些比较大的同城网站或论坛,就是里面都是武汉人的?
Laravel Sail是什么_基于Docker的Laravel本地开发环境Sail入门
如何自定义建站之星网站的导航菜单样式?
如何快速生成橙子建站落地页链接?
车管所网站制作流程,交警当场开简易程序处罚决定书,在交警网站查询不到怎么办?
laravel怎么为API路由添加签名中间件保护_laravel API路由签名中间件保护方法
网站页面设计需要考虑到这些问题
JavaScript实现Fly Bird小游戏
Laravel如何集成Inertia.js与Vue/React?(安装配置)
jquery插件bootstrapValidator表单验证详解
如何用AWS免费套餐快速搭建高效网站?
香港服务器部署网站为何提示未备案?
郑州企业网站制作公司,郑州招聘网站有哪些?
如何在 Pandas 中基于一列条件计算另一列的分组均值
php后缀怎么变mp4格式错误_修改扩展名提示格式不对怎么办【技巧】
Swift中swift中的switch 语句
如何快速查询网站的真实建站时间?
消息称 OpenAI 正研发的神秘硬件设备或为智能笔,富士康代工
如何在IIS中新建站点并配置端口与IP地址?
如何在Tomcat中配置并部署网站项目?
如何在阿里云虚拟服务器快速搭建网站?
Laravel Seeder怎么填充数据_Laravel数据库填充器的使用方法与技巧
Laravel如何升级到最新版本?(升级指南和步骤)
Bootstrap整体框架之JavaScript插件架构
网站制作免费,什么网站能看正片电影?
手机网站制作平台,手机靓号代理商怎么制作属于自己的手机靓号网站?
公司网站制作需要多少钱,找人做公司网站需要多少钱?
canvas 画布在主流浏览器中的尺寸限制详细介绍
如何快速登录WAP自助建站平台?
手机网站制作与建设方案,手机网站如何建设?
Laravel如何发送邮件和通知_Laravel邮件与通知系统发送步骤
如何登录建站主机?访问步骤全解析
简单实现Android文件上传
Midjourney怎么调整光影效果_Midjourney光影调整方法【指南】
HTML5段落标签p和br怎么选_文本排版常用标签对比【解答】
在线教育网站制作平台,山西立德教育官网?
Laravel如何获取当前登录用户信息_Laravel Auth门面使用与Session用户读取【技巧】
Laravel如何实现登录错误次数限制_Laravel自带LoginThrottles限流配置【方法】
HTML5空格和margin有啥区别_空格与外边距的使用场景【说明】
EditPlus中的正则表达式 实战(2)
Laravel安装步骤详细教程_Laravel环境搭建指南
Windows10电脑怎么设置虚拟光驱_Win10右键装载ISO镜像文件
北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?
Gemini怎么用新功能实时问答_Gemini实时问答使用【步骤】
如何实现javascript表单验证_正则表达式有哪些实用技巧
悟空浏览器如何设置小说背景色_悟空浏览器背景色设置【方法】
网页设计与网站制作内容,怎样注册网站?

