打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android中MeasureSpec介绍及使用详解

Android中MeasureSpec介绍及使用详解

分类: android动画学习 383人阅读 评论(0) 收藏 举报

一个MeasureSpec封装了父布局传递给子布局的布局要求,每个MeasureSpec代表了一组宽度和高度的要求。一个MeasureSpec由大小和模式组成。它有三种模式:UNSPECIFIED(未指定),父元素部队自元素施加任何束缚,子元素可以得到任意想要的大小;EXACTLY(完全),父元素决定自元素的确切大小,子元素将被限定在给定的边界里而忽略它本身大小;AT_MOST(至多),子元素至多达到指定大小的值。

它常用的三个函数:

  1.static int getMode(int measureSpec):根据提供的测量值(格式)提取模式(上述三个模式之一)

  2.static int getSize(int measureSpec):根据提供的测量值(格式)提取大小值(这个大小也就是我们通常所说的大小)

  3.static int makeMeasureSpec(int size,int mode):根据提供的大小值和模式创建一个测量值(格式)

这个类的使用呢,通常在view组件的onMeasure方法里面调用但也有少数例外,看看几个例子:

a.首先一个我们常用到的一个有用的函数,View.resolveSize(int size,int measureSpec)

public static int resolveSize(intsize, int measureSpec) {
    intresult = size;
    intspecMode = MeasureSpec.getMode(measureSpec);
    intspecSize =  MeasureSpec.getSize(measureSpec);
    switch(specMode) {
     caseMeasureSpec.UNSPECIFIED:
         result = size;
        break;
    caseMeasureSpec.AT_MOST:
         result = Math.min(size, specSize);
         break;
    caseMeasureSpec.EXACTLY:
        result = specSize;
         break;
     }
     returnresult;
 }
上面既然要用到measureSpec值,那自然表示这个函数通常是在onMeasure方法里面调用的。简单说一下,这个方法的主要作用就是根据你提供的大小和模式,返回你想要的大小值,这个里面根据传入模式的不同来做相应的处理。

再看看MeasureSpec.makeMeasureSpec方法,实际上这个方法很简单:

1
2
3
public static int makeMeasureSpec(int size, int mode) {
    returnsize + mode;
}

这样大家不难理解size跟measureSpec区别了。看看它的使用吧,ListView.measureItem(View child)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void measureItem(View child) {
         ViewGroup.LayoutParams p = child.getLayoutParams();
         if(p == null) {
             p =new ViewGroup.LayoutParams(
                     ViewGroup.LayoutParams.MATCH_PARENT,
                     ViewGroup.LayoutParams.WRAP_CONTENT);
         }
   
         intchildWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,
                 mListPadding.left + mListPadding.right, p.width);
         intlpHeight = p.height;
         intchildHeightSpec;
         if(lpHeight > 0) {
             childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
         }else {
             childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
         }
         child.measure(childWidthSpec, childHeightSpec);
     }

measureSpec方法通常在ViewGroup中用到,它可以根据模式(MeasureSpec里面的三个)可以调节子元素的大小。

注意,使用EXACTLY和AT_MOST通常是一样的效果,如果你要区别他们,那么你就要使用上面的函数View.resolveSize(int size,int measureSpec)返回一个size值,然后使用你的view调用setMeasuredDimension(int,int)函数。

1
2
3
4
5
6
protectedfinal void setMeasuredDimension(int measuredWidth, int measuredHeight) {
    mMeasuredWidth = measuredWidth;
    mMeasuredHeight = measuredHeight;
   
    mPrivateFlags |= MEASURED_DIMENSION_SET;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
图片适配的问题
View绘制过程
Android滚动截屏,ScrollView截屏,Listview截屏,Recyclerview截屏
覆写onMeaure进行measure操作
android.view.View
Android ViewGroup介绍+实例,apt编译时期自动生成代码&动态类加载
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服