Android 组合控件实现布局的复用的方法

看到很多项目会有实现自己的标题栏的做法,通常的界面是左边按钮或文字,加上中间的标题和右边的按钮或文字组成的。比较好的一种做法是使用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>

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

时间: 2024-08-03 03:33:42

Android 组合控件实现布局的复用的方法的相关文章

Android组合控件实现功能强大的自定义控件_Android

通常情况下,Android实现自定义控件无非三种方式. Ⅰ.继承现有控件,对其控件的功能进行拓展. Ⅱ.将现有控件进行组合,实现功能更加强大控件. Ⅲ.重写View实现全新的控件 上文说过了如何继承现有控件来自定义控件:<Android继承现有控件拓展实现自定义控件textView>,这节我们来讨论第二个议题.怎么将控件组合来实现一个功能强大的自定义控件. 先看看创建组合控件的好处吧,创建组合控件能够很好的创建具有组合功能的控件集合.那我们一般又是怎么做的了,一般我们来继承一个合适的ViewG

Android组合控件实现功能强大的自定义控件

通常情况下,Android实现自定义控件无非三种方式. Ⅰ.继承现有控件,对其控件的功能进行拓展. Ⅱ.将现有控件进行组合,实现功能更加强大控件. Ⅲ.重写View实现全新的控件 上文说过了如何继承现有控件来自定义控件:<Android继承现有控件拓展实现自定义控件textView>,这节我们来讨论第二个议题.怎么将控件组合来实现一个功能强大的自定义控件. 先看看创建组合控件的好处吧,创建组合控件能够很好的创建具有组合功能的控件集合.那我们一般又是怎么做的了,一般我们来继承一个合适的ViewG

Android使用控件ImageView加载图片的方法_Android

在 Android 加载图片一般使用 ImageView,这里简单记录一下这个控件的使用方法. 最简单就是在 xml 里直接使用 ImageView 标签: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="

android界面开发小结——android笔记---控件和布局

控件简介 ============================================================== 控件的设置主要依靠layout文件夹中的activity_main.xml设定   [html] view plaincopyprint? <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"       xmlns:tools="http:

Android使用控件ImageView加载图片的方法

在 Android 加载图片一般使用 ImageView,这里简单记录一下这个控件的使用方法. 最简单就是在 xml 里直接使用 ImageView 标签: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="

Android自定义控件之自定义组合控件(三)_Android

前言: 前两篇介绍了自定义控件的基础原理Android自定义控件基本原理详解(一).Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发成本,以及维护成本. 使用自定义组合控件的好处?我们在项目开发中经常会遇见很多相似或者相同的布局,比如APP的标题栏,我们从三种方式实现标题栏来对比自定义组件带来的好处,毕竟好的东西还是以提高开发效率,降低开发成本为导向的. 1.)第一种方式:直接在每个xml布局中写相同的标题栏布局代码  <?xml ve

Android中View自定义组合控件的基本编写方法_Android

有很多情况下,我们只要运用好Android给我提供好的控件,经过布局巧妙的结合在一起,就是一个新的控件,我称之为"自定义组合控件". 那么,这种自定义组合控件在什么情况下用呢?或者大家在做项目时候会发现,某些布局会被重复的利用,同一个布局的XML代码块会被重复的复制黏贴多次,这样会造成代码结构混乱不说,代码量也会增大,各种控件都需要在Java代码中被申明和处理相应的逻辑,工作量着实不小,所以,必须要找到一个合理的"偷懒"的方式,开动脑经去怎么简化以上说的不必要的麻烦

Android自定义控件之组合控件学习笔记分享_Android

我们来讲一下自定义组合控件,相信大家也接触过自定义组合控件吧,话不多说,直接干(哈~哈~): 大家看到这个觉得这不是很简单的吗,这不就是写个布局文件就搞定嘛,没错,确实直接上布局就行,不过,我只是用这个简单的例子来讲一下自定义组合控件的用法. 首先看看,这一行行的条目看起来都长得差不多,只是图片和文字不一样,没错,就是看中这一点,我们可以把一个条目做成一个组合控件,做为一个整体,这样不管你有几个条目,就写几个组合控件就行了. 步骤: 1.先建立组合控件的布局 myView.xml <Relati

Android自定义控件之组合控件学习笔记分享

我们来讲一下自定义组合控件,相信大家也接触过自定义组合控件吧,话不多说,直接干(哈~哈~): 大家看到这个觉得这不是很简单的吗,这不就是写个布局文件就搞定嘛,没错,确实直接上布局就行,不过,我只是用这个简单的例子来讲一下自定义组合控件的用法. 首先看看,这一行行的条目看起来都长得差不多,只是图片和文字不一样,没错,就是看中这一点,我们可以把一个条目做成一个组合控件,做为一个整体,这样不管你有几个条目,就写几个组合控件就行了. 步骤: 1.先建立组合控件的布局 myView.xml <Relati