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

因此,我们要通过自定义View,继承ViewGroup子类来实现这样的布局,降低布局文件和Activity代码耦合性。

首先,我们需要写出布局文件layout_custom_titlebar.xml。

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" />

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" />

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" />

2.定义自定义属性

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.正确地使用它

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

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" />

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="右边"/>

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="右边"/>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

android组合控件 重叠,Android 组合控件实现布局的复用的方法相关推荐

  1. c 用户控件 多语言,多语言文本控件重叠解决方案

    多语言文本控件重叠解决方案 文本控件重叠问题分析 文本控件重叠解决方案制定 在cocostudio编辑中做好前期基础准备工作 使用UIHelper.bindUIWidget自动化绑定UI布局 视图自动 ...

  2. android 屏幕分辨率 屏幕密度,Android屏幕适配——多分辨率多屏幕密度

    为什么要适配,适配的好处等等这里就不说了,直接说我们要怎么适配,请看下面的内容. 1.重要概念 px:pixel,像素Android原生API,UI设计计量单位,如获取屏幕宽高. 屏幕分辨率:指在纵向 ...

  3. Android 自定义View 三板斧之二——组合现有控件

    通常情况下,Android实现自定义控件无非三种方式. Ⅰ.继承现有控件,对其控件的功能进行拓展. Ⅱ.将现有控件进行组合,实现功能更加强大控件. Ⅲ.重写View实现全新的控件 上文说过了如何继承现 ...

  4. android 开发框架 怎么使用,Android快速开发框架dyh详解(二)---控件层的使用

    1.控件组合 1.1.ViewPager + Fragment, 这两个控件的组合在项目里也是比较常用的,效果同ViewPager,但由于是用Fragment所以可以使代码操作起来更方便: 1.1.1 ...

  5. android自定义控件不显示,解决Android Studio Design界面不显示layout控件的问题

    Android Studio更新到3.1.3后,发现拖到Design中的控件在预览界面中不显示: 解决办法: 在Styles.xml中的parent="..."中的Theme前添加 ...

  6. 重走Android路 之 挑几个基本控件玩玩(上卷)

    LZ-Says:冬瓜嫌弃LZ这个系列,说他都能看懂,哎呦我去,这个给人气的呀~真想把冬瓜剁吧剁吧熬成粥~ 高考了,祝愿各位学子功成归来~!!! 基本控件使用 Android为我们提供了很多UI样式,但 ...

  7. android shape 无边框颜色,Android 使用shape定义不同控件的的颜色、背景色、边框色...

    Android 使用shape定义不同控件的的颜色.背景色.边框色 设置按钮的右边框和底边框颜色为红色,边框大小为3dp: 在drawable新建一个 buttonstyle.xml的文件,内容如下: ...

  8. android 软键盘的从属关系,Android控件属性大全

    控件属性: android属性 Android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料, 第一类:属性值为true或false android:layout ...

  9. android 父控件的背景_android控件的属性

    android控件的属性 本节描述android空间的位置,内容等相关属性及属性的含义 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 ( ...

最新文章

  1. Java集合Stack源码深入解析
  2. hdu1978(递推dp)
  3. java程序设计_80后程序员,带你深入理解Java基本的程序设计结构,不来你别后悔...
  4. GitLab CI/CD
  5. 如何查询高考2021普体成绩,中考体育成绩对照表2020
  6. ssl提高组周三备考赛【2018.10.24】
  7. ORA-32004问题解决
  8. 斐波那契数列 青蛙跳台阶 变态跳台阶
  9. 局部变量是线程安全的,原因是什么
  10. 12. Docker修改默认存储位置
  11. javax.mail.MessagingException: 553 authentication is required
  12. SSM框架整合思想及步骤
  13. Cheat Engine 特征码
  14. IT蚁族:蜗居和逃离
  15. Gym - 101964E - Fishermen - (二分+区间更新)
  16. 数字信号处理之数字混频
  17. JavaScript——模拟自动饮料机
  18. MySQL008:数据库引擎,如何设置引擎独立空间
  19. 阿里云天池携手产学研心血管专家,共话心血管AI发展
  20. 对于文字设置超出部分隐藏

热门文章

  1. python学习笔记全过程_Python学习过程笔记整理(一)
  2. 石头剪刀布程序流程图_石头剪刀布!我要与电脑决战到天明!(14天)
  3. unity 敌人自动攻击和寻路_Unity暑期萌新入门:环境篇
  4. 图形驱动程序和显卡驱动什么区别_以后你的手机也需要单独安装显卡驱动程序了...
  5. 小企业文件打印服务器,小企业云服务器方案
  6. 微软codepush搭建服务器,通过 CodePush API 参考对本机 SDK 作出响应 - Visual Studio App Center | Microsoft Docs...
  7. slider获取点击 unity_Unity基础 | 70分钟带你轻松入门
  8. Win11任务栏一直转圈圈的解决方法
  9. qt更改类名_Qt编写自定义控件属性设计器
  10. springboot系列——redisTemplate和stringRedisTemplate对比、redisTemplate几种序列化方式比较