【移动应用开发技术】Android中怎么使用ViewGroup自定义布局_第1页
【移动应用开发技术】Android中怎么使用ViewGroup自定义布局_第2页
【移动应用开发技术】Android中怎么使用ViewGroup自定义布局_第3页
【移动应用开发技术】Android中怎么使用ViewGroup自定义布局_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论