Android自定义控件之自定义组合控件

Android自定义控件之自定义组合控件

前言:

前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一)、自定义属性Android自定义控件之自定义属性(二)。今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发成本,以及维护成本。

自定义控件相关文章地址:

Android自定义控件之基本原理

Android自定义控件之自定义属性

Android自定义控件之自定义组合控件

Android自定义控件之自定义ViewGroup实现标签云

使用自定义组合控件的好处?

我们在项目开发中经常会遇见很多相似或者相同的布局,比如APP的标题栏,我们从三种方式实现标题栏来对比自定义组件带来的好处,毕竟好的东西还是以提高开发效率,降低开发成本为导向的。

1.)第一种方式:直接在每个xml布局中写相同的标题栏布局代码

复制代码

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<RelativeLayout

android:layout_width="match_parent"

android:background="@color/green"

android:layout_height="45dp">

<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="@mipmap/titlebar_back_icon"

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:text="登录"

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:text="提交"

android:textColor="@android:color/white"

android:background="@null"

android:minHeight="45dp"

android:minWidth="45dp"

android:textSize="14sp" />

</RelativeLayout>

</LinearLayout>

复制代码

这种方式没有任何布局复用的概念,同时也让当前的布局变得臃肿难以维护,开发效率低下,而且这个还需要要求每个开发人员必须细心否则有可能会做出参差不齐的标题栏,所以这种方式是最不推荐使用的。

2.)第二种方式:使用include标签

首先定义标题栏布局

复制代码

<RelativeLayout

android:layout_width="match_parent"

android:background="@color/green"

android:layout_height="45dp">

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

</RelativeLayout>

复制代码

然后在需要的地方通过include标签实现引用

复制代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<include layout="@layout/view_title_bar" />

</LinearLayout>

复制代码

通过上面的布局代码,我们可以使用上面这种方式确实实现了布局的复用,而且也避免了开发人员开发出参差不齐标题栏的问题,但是同时也引入了新的问题,比如更加降低了开发效率,加大了开发成本,问题就在我们该如何为每个布局文件定义标题栏?只有通过代码的方式来设置标题问题,左右按钮等其他的属性,导致布局属性和Activity代码耦合性比较高,所以这种方式也不是推荐的方式。

3.)第三种方式:通过自定义组合控件

这里先不具体介绍如何实现一个自定义组合控件,这里先介绍一下自定义组合控件带来的好处。

提高布局文件开发效率

降低布局文件维护成本

降低布局文件和Activity代码耦合性

容易扩展

简单易用

如何实现一个自定义组合控件

1.)先定义一个布局文件

复制代码

<merge xmlns:android="http://schemas.android.com/apk/res/android">

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

复制代码

注意:这里为何要使用merge标签,自定义组合控件时会继承RelativeLayout、LinearLayout等控件,这样导致布局的层级无形中增加了一层,如下是对比:

未使用merge标签

使用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="right_button_text" format="string" />

<attr name="right_button_text_color" format="color" />

<attr name="right_button_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" />

</declare-styleable>

复制代码

3.)自定义一个View根据需求继承不同的ViewGroup子类,比如:RelativeLayout、LinearLayout等,我们这里继承RelativeLayout。

复制代码

public class CustomTitleBar extends RelativeLayout {

private Button titleBarLeftBtn;

private Button titleBarRightBtn;

private TextView titleBarTitle;

public CustomTitleBar(Context context, AttributeSet attrs) {

super(context, attrs);

LayoutInflater.from(context).inflate(R.layout.custom_title_bar, 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 attributes = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleBar);

if (attributes != null) {

//处理titleBar背景色

int titleBarBackGround = attributes.getResourceId(R.styleable.CustomTitleBar_tlb_title_background_color, Color.GREEN);

setBackgroundResource(titleBarBackGround);

//先处理左边按钮

//获取是否要显示左边按钮

boolean leftButtonVisible = attributes.getBoolean(R.styleable.CustomTitleBar_tlb_left_button_visible, true);

if (leftButtonVisible) {

titleBarLeftBtn.setVisibility(View.VISIBLE);

} else {

titleBarLeftBtn.setVisibility(View.INVISIBLE);

}

//设置左边按钮的文字

String leftButtonText = attributes.getString(R.styleable.CustomTitleBar_tlb_left_button_text);

if (!TextUtils.isEmpty(leftButtonText)) {

titleBarLeftBtn.setText(leftButtonText);

//设置左边按钮文字颜色

int leftButtonTextColor = attributes.getColor(R.styleable.CustomTitleBar_tlb_left_button_text_color, Color.WHITE);

titleBarLeftBtn.setTextColor(leftButtonTextColor);

}

//设置左边图片icon 这里是二选一 要么只能是文字 要么只能是图片

int leftButtonDrawable = attributes.getResourceId(R.styleable.CustomTitleBar_tlb_left_button_drawable, R.mipmap.titlebar_back_icon);

if (leftButtonDrawable != -1) {

titleBarLeftBtn.setCompoundDrawablesWithIntrinsicBounds(leftButtonDrawable, 0, 0, 0); //设置到哪个控件的位置()

}

//处理标题

//先获取标题是否要显示图片icon

int titleTextDrawable = attributes.getResourceId(R.styleable.CustomTitleBar_tlb_title_text_drawable, -1);

if (titleTextDrawable != -1) {

titleBarTitle.setBackgroundResource(titleTextDrawable);

} else {

//如果不是图片标题 则获取文字标题

String titleText = attributes.getString(R.styleable.CustomTitleBar_tlb_title_text);

if (!TextUtils.isEmpty(titleText)) {

titleBarTitle.setText(titleText);

}

//获取标题显示颜色

int titleTextColor = attributes.getColor(R.styleable.CustomTitleBar_tlb_title_text_color, Color.WHITE);

titleBarTitle.setTextColor(titleTextColor);

}

//先处理右边按钮

//获取是否要显示右边按钮

boolean rightButtonVisible = attributes.getBoolean(R.styleable.CustomTitleBar_tlb_right_button_visible, true);

if (rightButtonVisible) {

titleBarRightBtn.setVisibility(View.VISIBLE);

} else {

titleBarRightBtn.setVisibility(View.INVISIBLE);

}

//设置右边按钮的文字

String rightButtonText = attributes.getString(R.styleable.CustomTitleBar_tlb_right_button_text);

if (!TextUtils.isEmpty(rightButtonText)) {

titleBarRightBtn.setText(rightButtonText);

//设置右边按钮文字颜色

int rightButtonTextColor = attributes.getColor(R.styleable.CustomTitleBar_tlb_right_button_text_color, Color.WHITE);

titleBarRightBtn.setTextColor(rightButtonTextColor);

}

//设置右边图片icon 这里是二选一 要么只能是文字 要么只能是图片

int rightButtonDrawable = attributes.getResourceId(R.styleable.CustomTitleBar_tlb_right_button_drawable, -1);

if (rightButtonDrawable != -1) {

titleBarRightBtn.setCompoundDrawablesWithIntrinsicBounds(0, 0, rightButtonDrawable, 0); //设置到哪个控件的位置()

}

attributes.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布局中引用

关于如何使用自定义属性这里就不再说明了,为了更加直观的查看效果,我这里在一个布局文件中实现不同要求的标题栏

复制代码

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<com.whoislcj.views.CustomTitleBar

android:layout_width="match_parent"

android:layout_height="45dp"

android:layout_marginTop="10dp"

lee:right_button_drawable="@mipmap/titlebar_add_icon"

lee:title_background_color="@color/green"

lee:title_text="标题1" />

<com.whoislcj.views.CustomTitleBar

android:layout_width="match_parent"

android:layout_height="45dp"

android:layout_marginTop="10dp"

lee:right_button_visible="false"

lee:title_background_color="@color/green"

lee:title_text="标题2" />

<com.whoislcj.views.CustomTitleBar

android:layout_width="match_parent"

android:layout_height="45dp"

android:layout_marginTop="10dp"

lee:left_button_text="左边"

lee:right_button_text="右边"

lee:title_background_color="@color/red"

lee:title_text="标题3" />

<com.whoislcj.views.CustomTitleBar

android:layout_width="match_parent"

android:layout_height="45dp"

android:layout_marginTop="10dp"

lee:left_button_text="左边"

lee:right_button_drawable="@mipmap/titlebar_add_icon"

lee:title_background_color="@color/red"

lee:title_text="标题4" />

<com.whoislcj.views.CustomTitleBar

android:layout_width="match_parent"

android:layout_height="45dp"

android:layout_marginTop="10dp"

lee:left_button_text="左边"

lee:left_button_text_color="@color/red"

lee:right_button_drawable="@mipmap/titlebar_add_icon"

lee:title_background_color="@color/blue"

lee:title_text="标题5" />

<com.whoislcj.views.CustomTitleBar

android:layout_width="match_parent"

android:layout_height="45dp"

android:layout_marginTop="10dp"

lee:left_button_text="左边"

lee:left_button_text_color="@color/red"

lee:right_button_drawable="@mipmap/titlebar_add_icon"

lee:title_background_color="@color/blue"

lee:title_text="标题6"

lee:title_text_color="@color/black" />

</LinearLayout>

复制代码

显示效果

[图片上传失败...(image-e6ac8e-1529929140024)]

总结:

通过本篇文章我们得知,通过自定义组合控件确实能够提高开发效率,降低维护成本,但是也需要UI设计风格保持高度一致,不然的话只能呵呵哒了!所以想要做好一个app需要一个有共识的团队才行。本篇介绍到此为止,下一篇要更新什么我还没有想好!有可能是自定义控件的事件回调,也有可能自定义ViewGroup实现流式布局。

MyCustomView

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,199评论 0 17
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    wgl0419阅读 6,116评论 1 9
  • 【Android 自定义View】 [TOC] 自定义View基础 接触到一个类,你不太了解他,如果贸然翻阅源码只...
    Rtia阅读 3,895评论 1 14
  • 天空澄明空远 一出永恒的悲剧之幕 肮脏的躯体 死亡路上绽放璀璨的光芒 大地静穆深沉 一座没有时空限制的舞台 主角是...
    楚天客阅读 514评论 10 13
  • 我们只是在花未开好的时刻遇到了对方,于是就变成了,我的自以为是,你的视而不见。
    清欢南叙阅读 209评论 0 0