![【移动应用开发技术】Android中怎么使用ViewGroup自定义布局_第1页](http://file4.renrendoc.com/view/d24bc999e80429c29876485df510b4ce/d24bc999e80429c29876485df510b4ce1.gif)
![【移动应用开发技术】Android中怎么使用ViewGroup自定义布局_第2页](http://file4.renrendoc.com/view/d24bc999e80429c29876485df510b4ce/d24bc999e80429c29876485df510b4ce2.gif)
![【移动应用开发技术】Android中怎么使用ViewGroup自定义布局_第3页](http://file4.renrendoc.com/view/d24bc999e80429c29876485df510b4ce/d24bc999e80429c29876485df510b4ce3.gif)
![【移动应用开发技术】Android中怎么使用ViewGroup自定义布局_第4页](http://file4.renrendoc.com/view/d24bc999e80429c29876485df510b4ce/d24bc999e80429c29876485df510b4ce4.gif)
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android中怎么使用ViewGroup自定义布局
本篇文章为大家展示了Android中怎么使用ViewGroup自定义布局,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。步骤这里我为大家设计一个类似LinearLayout线性布局的ViewGroup作为范例。首先,如果是一个LinearLayout那么当设置wrap_content时,他就会以子空间中最宽的那个为它的宽度。同时在高度方面会是所有子控件高度的总和。所以我们先写两个方法,分别用于测量ViewGroup的宽度和高度。
private
int
getMaxWidth(){
int
count
=
getChildCount();
int
maxWidth
=
0;
for
(int
i
=
0
;
i
<
count
;
i
++){
int
currentWidth
=
getChildAt(i).getMeasuredWidth();
if
(maxWidth
<
currentWidth){
maxWidth
=
currentWidth;
}
}
return
maxWidth;
}
private
int
getTotalHeight(){
int
count
=
getChildCount();
int
totalHeight
=
0;
for
(int
i
=
0
;
i
<
count
;
i++){
totalHeight
+=
getChildAt(i).getMeasuredHeight();
}
return
totalHeight;
}对于ViewGroup而言我们可以粗略的分为两种模式:固定长宽模式(match_parent),自适应模式(wrap_content),根据这两种模式,就可以对ViewGroup的绘制进行划分。这里关于measureChildren这个方法,他是用于将所有的子View进行测量,这会触发每个子View的onMeasure函数,但是大家要注意要与measureChild区分,measureChild是对单个view进行测量
@Override
protected
void
onMeasure(int
widthMeasureSpec,
int
heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec,
heightMeasureSpec);
measureChildren(widthMeasureSpec,
heightMeasureSpec);
int
widthMode
=
MeasureSpec.getMode(widthMeasureSpec);
int
width
=
MeasureSpec.getSize(widthMeasureSpec);
int
heightMode=
MeasureSpec.getMode(heightMeasureSpec);
int
height
=
MeasureSpec.getSize(heightMeasureSpec);
if
(widthMode
==
MeasureSpec.AT_MOST
&&
heightMode
==
MeasureSpec.AT_MOST){
int
groupWidth
=
getMaxWidth();
int
groupHeight=
getTotalHeight();
setMeasuredDimension(groupWidth,
groupHeight);
}else
if
(widthMode
==
MeasureSpec.AT_MOST){
setMeasuredDimension(getMaxWidth(),
height);
}else
if
(heightMode
==
MeasureSpec.AT_MOST){
setMeasuredDimension(width,
getTotalHeight());
}
}重写onLayout整完上面这些东西,我们的布局大小七十九已经出来了,然我们在活动的布局文件里面加上它,并添加上几个子View然后运行一下,先看看效果:
<com.entry.android_view_user_defined_first.views.MyLinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent">
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="qwe"/>
<Button
android:layout_width="250dp"
android:layout_height="150dp"
android:text="qwe"/>
<Button
android:layout_width="200dp"
android:layout_height="75dp"
android:text="qwe"/>
</com.entry.android_view_user_defined_first.views.MyLinearLayout>运行效果如下:我们看见布局出来了,大小好像也没啥问题,但是子View呢??!这么没看见子View在看看代码,系统之前然我们重写的onLayout()还是空着的呀!!也就是说,子View的大小和位置根本就还没有进行过设定!让我们来重写下onLayout()方法。
@Override
protected
void
onLayout(boolean
changed,
int
l,
int
t,
int
r,
int
b)
{
int
count
=
getChildCount();
int
currentHeight
=
0;
for
(int
i
=
0
;
i
<
count
;
i++){
View
view
=
getChildAt(i);
int
height
=
view.getMeasuredHeigh
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 三位数除以两位数过关练习试题带答案
- 小学三年级数学三位数乘以一位数质量练习例题大全附答案
- 四年级英语下册 Unit 2 My family Part B第三课时说课稿2 人教PEP
- 农村房屋装潢合同范例
- 仓库场地长期租赁合同范例
- 东坑送菜公司合同范本
- 合同范例遗赠协议
- 2025年建筑辅材施工合同模板
- 2025年度智能设备研发合作经营权合同范本
- 2025年度合同管理台账范本:物流运输合同管理
- 手术安全管理之手术部位标识安全
- 2022年版煤矿安全规程
- 高质量社区建设的路径与探索
- 数字化时代的酒店员工培训:技能升级
- 足球守门员扑救技巧:扑救结合守护球门安全
- 《学术规范和论文写作》课件全套 第1-10章 知:认识研究与论文写作 - 引文规范
- 带式输送机滚筒出厂检验规范
- 起重机更换卷筒施工方案
- 《信息检索基础知识》课件
- 具有履行合同所必须的设备和专业技术能力的承诺函-设备和专业技术能力承诺
- 01智慧物流信息技术概述
评论
0/150
提交评论