Android开发实践:自定义ViewGroup的onLayout()分析
Android开发中,对于自定义View,分为两种,一种是自定义控件(继承View类),另一种是自定义布局容器(继承ViewGroup)。如果是自定义控件,则一般需要重载两个方法,一个是onMeasure(),用来测量控件尺寸,另一个是onDraw(),用来绘制控件的UI。而自定义布局容器,则一般需要实现/重载三个方法,一个是onMeasure(),也是用来测量尺寸;一个是onLayout(),用来布局子控件;还有一个是dispatchDraw(),用来绘制UI。
本文主要分析自定义ViewGroup的onLayout()方法的实现。
ViewGroup类的onLayout()函数是abstract型,继承者必须实现,由于ViewGroup的定位就是一个容器,用来盛放子控件的,所以就必须定义要以什么的方式来盛放,比如LinearLayout就是以横向或者纵向顺序存放,而RelativeLayout则以相对位置来摆放子控件,同样,我们的自定义ViewGroup也必须给出我们期望的布局方式,而这个定义就通过onLayout()函数来实现。
我们通过实现一个水平优先布局的视图容器来更加深入地了解onLayout()的实现吧,效果如图所示(黑色方块为子控件,白色部分为自定义布局容器)。该容器的布局方式是,首先水平方向上摆放子控件,水平方向放不下了,则另起一行继续水平摆放。
1. 自定义ViewGroup的派生类
第一步,则是自定ViewGroup的派生类,继承默认的构造函数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class CustomViewGroup extends ViewGroup {
public CustomViewGroup(Context context) {
super (context);
}
public CustomViewGroup(Context context, AttributeSet attrs) {
super (context, attrs);
}
public CustomViewGroup(Context context, AttributeSet attrs, intdefStyle) {
super (context, attrs, defStyle);
}
}
|
2. 重载onMeasure()方法
为什么要重载onMeasure()方法这里就不赘述了,上一篇文章已经讲过,这里需要注意的是,自定义ViewGroup的onMeasure()方法中,除了计算自身的尺寸外,还需要调用measureChildren()函数来计算子控件的尺寸。
onMeasure()的定义不是本文的讨论重点,因此这里我直接使用默认的onMeasure()定义,当然measureChildren()是必须得加的。
1
2
3
4
5
|
@Override
protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) {
super .onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
}
|
3. 实现onLayout()方法
onLayout()函数的原型如下:
1
2
3
|
//@param changed 该参数指出当前ViewGroup的尺寸或者位置是否发生了改变
//@param left top right bottom 当前ViewGroup相对于其父控件的坐标位置
protected void onLayout( boolean changed, int left, int top, int right, int bottom);
|
由于我们希望优先横向布局子控件,那么,首先,我们知道总宽度是多少,这个值可以通过getMeasuredWidth()来得到,当然子控件的宽度也可以通过子控件对象的getMeasuredWidth()来得到。
这样,就不复杂了,具体的实现代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
protected void onLayout( boolean changed, int left, int top, int right, int bottom) {
int mViewGroupWidth = getMeasuredWidth(); //当前ViewGroup的总宽度
int mPainterPosX = left; //当前绘图光标横坐标位置
int mPainterPosY = top; //当前绘图光标纵坐标位置
int childCount = getChildCount();
for ( int i = 0 ; i < childCount; i++ ) {
View childView = getChildAt(i);
int width = childView.getMeasuredWidth();
int height = childView.getMeasuredHeight();
//如果剩余的空间不够,则移到下一行开始位置
if ( mPainterPosX + width > mViewGroupWidth ) {
mPainterPosX = left;
mPainterPosY += height;
}
//执行ChildView的绘制
childView.layout(mPainterPosX,mPainterPosY,mPainterPosX+width, mPainterPosY+height);
//记录当前已经绘制到的横坐标位置
mPainterPosX += width;
}
}
|
4. 布局文件测试
下面我们就尝试写一个简单的xml文件,来测试一下我们的自定义ViewGroup,我们把子View的背景颜色都设置为黑色,方便我们辨识。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<com.titcktick.customview.CustomViewGroup xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
android:layout_width= "match_parent"
android:layout_height= "match_parent" >
<View
android:layout_width= "100dp"
android:layout_height= "100dp"
android:layout_margin= "10dp"
android:background= "@android:color/black" />
<View
android:layout_width= "100dp"
android:layout_height= "100dp"
android:layout_margin= "10dp"
android:background= "@android:color/black" />
<View
android:layout_width= "100dp"
android:layout_height= "100dp"
android:layout_margin= "10dp"
android:background= "@android:color/black" />
<View
android:layout_width= "100dp"
android:layout_height= "100dp"
android:layout_margin= "10dp"
android:background= "@android:color/black" />
</com.titcktick.customview.CustomViewGroup>
|
5. 添加layout_margin
为了让核心逻辑更加清晰,上面的onLayout()实现我隐去了margin的计算,这样就会导致子控件的layout_margin不起效果,所以上述效果是子控件一个个紧挨着排列,中间没有空隙。那么,下面我们来研究下如何添加margin效果。
其实,如果要自定义ViewGroup支持子控件的layout_margin参数,则自定义的ViewGroup类必须重载generateLayoutParams()函数,并且在该函数中返回一个ViewGroup.MarginLayoutParams派生类对象,这样才能使用margin参数。
ViewGroup.MarginLayoutParams的定义关键部分如下,它记录了子控件的layout_margin值:
1
2
3
4
5
6
|
public static class MarginLayoutParams extends ViewGroup.LayoutParams {
public int leftMargin;
public int topMargin;
public int rightMargin;
public int bottomMargin;
}
|
你可以跟踪源码看看,其实XML文件中View的layout_xxx参数都是被传递到了各种自定义ViewGroup.LayoutParams派生类对象中。例如LinearLayout的LayoutParams定义的关键部分如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class LinearLayout extends ViewGroup {
public static class LayoutParams extends ViewGroup.MarginLayoutParams {
public float weight;
public int gravity = - 1 ;
public LayoutParams(Context c, AttributeSet attrs) {
super (c, attrs);
TypedArray a = c.obtainStyledAttributes(attrs, com.android.internal.R.styleable.LinearLayout_Layout);
weight = a.getFloat(com.android.internal.R.styleable.LinearLayout_Layout_layout_weight, 0 );
gravity = a.getInt(com.android.internal.R.styleable.LinearLayout_Layout_layout_gravity, - 1 );
a.recycle();
}
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LinearLayout.LayoutParams(getContext(), attrs);
}
}
|
这样你大概就可以理解为什么LinearLayout的子控件支持weight和gravity的设置了吧,当然我们也可以这样自定义一些属于我们ViewGroup特有的params,这里就不详细讨论了,我们只继承MarginLayoutParams来获取子控件的margin值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class CustomViewGroup extends ViewGroup {
public static class LayoutParams extends ViewGroup.MarginLayoutParams {
public LayoutParams(Context c, AttributeSet attrs) {
super (c, attrs);
}
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new CustomViewGroup.LayoutParams(getContext(), attrs);
}
}
|
这样修改之后,我们就可以在onLayout()函数中获取子控件的layout_margin值了,添加了layout_margin的onLayout()函数实现如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
@Override
protected void onLayout( boolean changed, int left, int top, int right, int bottom) {
int mViewGroupWidth = getMeasuredWidth(); //当前ViewGroup的总宽度
int mViewGroupHeight = getMeasuredHeight(); //当前ViewGroup的总高度
int mPainterPosX = left; //当前绘图光标横坐标位置
int mPainterPosY = top; //当前绘图光标纵坐标位置
int childCount = getChildCount();
for ( int i = 0 ; i < childCount; i++ ) {
View childView = getChildAt(i);
int width = childView.getMeasuredWidth();
int height = childView.getMeasuredHeight();
CustomViewGroup.LayoutParams margins = (CustomViewGroup.LayoutParams)(childView.getLayoutParams());
//ChildView占用的width = width+leftMargin+rightMargin
//ChildView占用的height = height+topMargin+bottomMargin
//如果剩余的空间不够,则移到下一行开始位置
if ( mPainterPosX + width + margins.leftMargin + margins.rightMargin > mViewGroupWidth ) {
mPainterPosX = left;
mPainterPosY += height + margins.topMargin + margins.bottomMargin;
}
//执行ChildView的绘制
childView.layout(mPainterPosX+margins.leftMargin, mPainterPosY+margins.topMargin,mPainterPosX+margins.leftMargin+width, mPainterPosY+margins.topMargin+height);
mPainterPosX += width + margins.leftMargin + margins.rightMargin;
}
}
|
本文转自 Jhuster 51CTO博客,原文链接:http://blog.51cto.com/ticktick/1542200,如需转载请自行联系原作者
Android开发实践:自定义ViewGroup的onLayout()分析相关推荐
- Xamarin.Android开发实践(十四)
原文:Xamarin.Android开发实践(十四) Xamarin.Android之ListView和Adapter 一.前言 如今不管任何应用都能够看到列表的存在,而本章我们将学习如何使用Xama ...
- android自定义横竖双向滚动,Android开发实现自定义水平滚动的容器示例
Android开发实现自定义水平滚动的容器示例 发布时间:2020-09-12 01:25:56 来源:脚本之家 阅读:71 作者:CharlinGod 本文实例讲述了Android开发实现自定义水平 ...
- android实现新闻内容显示功能,Android开发实现自定义新闻加载页面功能实例
本文实例讲述了Android开发实现自定义新闻加载页面功能.分享给大家供大家参考,具体如下: 一.概述: 1.效果演示: 2.说明:在新闻页面刚加载的时候,一般会出现五种状态 未知状态(STATE_U ...
- Xamarin.Android开发实践(十七)
Xamarin.Android开发实践(十七) 原文:Xamarin.Android开发实践(十七) Xamarin.Android之定位 一.前言 打开我们手中的应用,可以发现越来越多的应用使用了定 ...
- Xamarin.Android开发实践(一)
原文:Xamarin.Android开发实践(一) 一.准备工作 1.创建一个空的解决方案,并命名为Phoneword 2.右击解决方案 新建->新建项目 并命名为Phoneword_Droid ...
- Android开发实践:Java层与Jni层的数组传递
Android开发中,经常会在Java代码与Jni层之间传递数组(byte[]),一个典型的应用是Java层把需要发送给客户端的数据流传递到Jni层,由Jni层的Socket代码发送出去,当然,Jni ...
- 《Android开发卷——自定义日期选择器(三)》
继 <Android开发卷--自定义日期选择器(一)>:http://blog.csdn.net/chillax_li/article/details/19047 ...
- 《Android开发卷——自定义日期选择器(二)》
(小米手机) (中兴手机) 在上一篇中,我介绍了一般公司都会自定义时间日期选择器,并结合自己所做的项目给大家参考. 工作实录之<Android开发卷--自定义日期选择器(一)>链接:htt ...
- android并发命令,Android开发实践:基于命令模式的异步任务线程
关于Android的异步操作,我在文章<Android开发实践:线程与异步任务>中介绍了两种方法,一种是采用线程,另一种是采用AsyncTask,今天再深入探讨下另一种模型:命令式的异步任 ...
最新文章
- 物联网技术正颠覆传统医疗行业
- 在线作图|2分钟绘制一张箱线图
- 原创 | 一文读懂正态分布与贝塔分布
- 基于springboot2.x集成缓存注解及设置过期时间
- UPLOOKING_APUE
- Ubuntu root密码
- 前端学习(5):深入了解网站开发
- Openstack 一直在调度中解决
- 用js实现一个无限循环的动画
- win7 oracle数据库删除用户名,图文帮你win7系统删除Oracle数据库中的用户的具体方法...
- 活动选择问题 贪心
- 三菱M80加工中心伺服电机调试软件带序列号
- oeasy教您玩转vim - 005 - # 程序本质
- 力扣刷题 DAY_81 贪心
- 声学模型(语音识别中的)--学习笔记
- 智能音箱天猫精灵使用体验--写在前面的话
- 在树莓派上安装麦克风监测音量
- java中的NIO,BIO,AIO
- 2013c语言二级等级考试试题,计算机等级考试二级c语言考试试题
- hive sql通过具体地址解析出行政区划(省 > 市 > 区 > 县 > 乡 > 镇 > 村)