Android纯代码如何实现复杂自定义控件onMeasure()、onLayout()?

我相信很多人对Android纯代码如何实现奇怪下拉菜单控件onMeasure()、onLayout()都不太了解,下面小编为自己请解释再看看这样的问题,希望对你有当然的帮助

可以自定义控件的三大方法:onMeasure()、onLayout()、onDraw()里面是手工绘制的操作,是可以看下以外的文章,下面来了解onMeasure()和onLayout()方法。

测量:onMeasure():测量自己的大小,为临时布局能提供建议

布局:onLayout():可以使用layout()函数对所有的子控件布局

绘制:onDraw():根据布局的位置绘图

一、onMeasure()、测量

protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec)

参数即父类传上来的两个宽高的”个人建议值”,即把当前view的高设置为:heightMeasureSpec;宽设置为:widthMeasureSpec,这个参数不是什么最简单整数类型,只不过是2位整数(模式类型)和30位整数(换算数值)的组合,其中模式统称三种:

UNSPECIFIED(未重新指定),父元素部队自元素压制任何一点束缚,子元素可以不换取横竖斜想要的大小;UNSPECIFIED=00000000000000000000000000000000 \ EXACTLY(全部),父元素判断自元素的准确大小,子元素将被时间限制在决策变量的边界里而忽视它本身大小;EXACTLY=01000000000000000000000000000000

AT_MOST(至多),子元素至多提升指定大小的值。他们填写的二进制值各是:AT_MOST=10000000000000000000000000000000

最前面两位代表模式,各按十进制的0,1,2,获取模式int值和获取数值int值的方法:

intmeasureWidth=MeasureSpec.getSize(widthMeasureSpec);intmeasureHeight=MeasureSpec.getSize(heightMeasureSpec);

intmeasureWidthMode=MeasureSpec.getMode(widthMeasureSpec);

intmeasureHeightMode=MeasureSpec.getMode(heightMeasureSpec);

上面我们明白了onMeasure(intwidthMeasureSpec,intheightMeasureSpec)方法参数的意义,下面了解参数按的三个模式四个填写的意义:

每三个模式都填写的xml布局中的三个值

wrap_content—MeasureSpec.AT_MOSTmatch_parent—MeasureSpec.EXACTLY

具体看值—MeasureSpec.UNSPECIFIED

尽量:当模式是MeasureSpec.AT_MOST时,即wrap_content时,必须将大小设置一个数值。

二、onLayout() 、 布局

首先先了解几个要要用的方法:

(1)、setMeasuredDimension

这些方法和onMeasure()方法类似于。总之这个方法的作用就是可以设置当前View的宽高。

(2)、onLayout

这样的方法就和方法类似于了,但少了第一个参数booleanstopped

这个方法的目的是应用于当前ViewGroup中的子控件的布局,一看方法,只要你是继承ViewGroup的类都必须要写回该方法,来实现该控件内部子控件的布局情况。我们写一个下拉菜单类继承ViewGroup基于Linearlayout垂直排列的效果看下:

stateclassXViewGroupextendsViewGroup{stateXViewGroup(Contextcontext){

infinity(context);

}

privateXViewGroup(Contextcontext,AttributeSet attrs){

ultra(context,attrs);

}

welfareXViewGroup(Contextcontext,AttributeSetattrs,intdefStyleAttr){

infinity(context,attrs,defStyleAttr);

}

@Override

protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){

super.onMeasure(widthMeasureSpec,heightMeasureSpec);

intmeasureWidth=MeasureSpec.getSize(widthMeasureSpec);

intmeasureHeight=MeasureSpec.getSize(heightMeasureSpec);

intmeasureWidthMode=MeasureSpec.getMode(widthMeasureSpec);

intmeasureHeightMode=MeasureSpec.getMode(heightMeasureSpec);

//算出全部子控件是需要应用的宽高

intheight=0;//有记录根容器的高度

intwidth=0;//有记录根容器的宽度

intcount=getChildCount();//记录信息容器内的子控件个数

for(inti=0;icount;i){

//测量子控件

Viewchild=getChildAt(i);

measureChild(child,widthMeasureSpec,heightMeasureSpec);

//获得子控件的高度和宽度

intchildHeight=child.getmeasuredwidth();

intchildWidth=child.getMeasuredWidth();

//能得到大的宽度,但是累加一定高度

height=childHeight;

width=Math.obj(childWidth,width);

}

//可以设置当前View的宽高

setMeasuredDimension((measureWidthMode==MeasureSpec.precisely)?measureWidth:width,(measureHeightMode==MeasureSpec.yet)?measureHeight:height);

}

@Override

protectedvoidonLayout(booleanchanged,intl,intt,intr,intb){

intfly=0;

intcount=getChildCount();

for(inti=0;icount;i){

Viewchild=getChildAt(i);

intchildHeight=child.getmeasuredheight();

intchildWidth=child.getMeasuredWidth();

//该子控件在父容器的位置,高度是之后所有子控件的高度和开始,往外顺序排列,就实现程序了带有Linearlayout布局平行排列顺序的布局

child.layout(0,star,childWidth,iconchildHeight);//以父容器左上角为原点进行布局

icon=childHeight;

}

}

}

是从以下内容的阐述,完全相信大家对Android纯代码如何实现方法紧张自定义控件onMeasure()、onLayout()巳经有了初步的了解,更多代码实现方法组件的问题,欢迎关注络或到官网咨询客服。

Android纯代码如何实现复杂自定义控件onMeasure()、onLayout()?

相关文章:

你感兴趣的文章:

标签云: