版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android开发实践:自定义ViewGroup的onLayout()分析
前一篇文章主要讲了自定义View为什么要重载onMeasure()方法,那么,自定义ViewGroup又都有哪些方法需要重载或者实现呢?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的派生类,继承默认的构造函数。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()是必须得加的。@Override
protected
void
onMeasure(int
widthMeasureSpec,
int
heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec,
heightMeasureSpec);
measureChildren(widthMeasureSpec,
heightMeasureSpec);
}
3.
实现onLayout()方法
onLayout()函数的原型如下://@param
changed
该参数指出当前ViewGroup的尺寸或者位置是否发生了改变
//@param
left
top
right
bottom
当前ViewGroup相对于其父控件的坐标位置
protected
void
onLayout(boolean
changed,int
left,
int
top,
int
right,
int
bottom);
由于我们希望优先横向布局子控件,那么,首先,我们知道总宽度是多少,这个值可以通过getMeasuredWidth()来得到,当然子控件的宽度也可以通过子控件对象的getMeasuredWidth()来得到。
这样,就不复杂了,具体的实现代码如下所示: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的背景颜色都设置为黑色,方便我们辨识。<com.titcktick.customview.CustomViewGroup
xmlns:android="/apk/res/android"
xmlns:tools="/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值: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定义的关键部分如下: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,
ernal.R.styleable.LinearLayout_Layout);
weight
=
a.getFloat(ernal.R.styleable.LinearLayout_Layout_layout_weight,
0);
gravity
=
a.getInt(ernal.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值。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()函数实现如下所示:@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
//ChildV
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026重庆青年镇招聘公益性岗位人员4人备考题库含完整答案详解(网校专用)
- 2026浙江丽水市松阳县事业单位招聘39人备考题库附参考答案详解【a卷】
- 2026湖南长沙市芙蓉区招聘中小学教师41人备考题库及参考答案详解(巩固)
- 2026松原吉林油田医院招聘38人备考题库含答案详解【研优卷】
- 2026广西桂林市社会保险事业管理中心招聘公益性岗位人员1人备考题库附完整答案详解【各地真题】
- 2026江苏南通市第一人民医院第一批招聘备案制工作人员102人备考题库附完整答案详解(各地真题)
- 2026陕西西安市高新第一学校招聘备考题库及完整答案详解(网校专用)
- 2026宁夏银川永宁县卫生健康系统专业技术人员自主招聘59人备考题库【名校卷】附答案详解
- 中移动金融科技有限公司2026春季园招聘备考题库附答案详解【巩固】
- 2026浙江杭州市西湖区云浦幼儿园招聘幼儿教师备考题库(非事业)带答案详解(满分必刷)
- 2025届中烟机械技术中心高校毕业生招聘2人(第二批次)笔试参考题库附带答案详解
- 高压配电房设备定期维护保养记录表格
- 屠宰企业食品安全知识培训课件
- 《市场监督管理投诉举报处理办法》知识培训
- 卵巢黄体囊肿破裂课件
- 物业扭亏为盈工作汇报
- 2025广东中考短文填空公开课
- 《AutoCAD 2025中文版实例教程(微课版)》全套教学课件
- 入职性格测试题目及答案
- 艾滋病考试题及答案超星
- 化工设备的安全评估
评论
0/150
提交评论