`
懒骨头
  • 浏览: 44395 次
  • 性别: Icon_minigender_1
  • 来自: 南通
社区版块
存档分类
最新评论

View 中 onMeasure 测试

阅读更多

在View中,onMeasure是父类调用,用来计算该View的显示空间,但是一直不理解,MeasureSpec.EXACTLY,MeasureSpec.AT_MOST,MeasureSpec.UNSPECIFIED是什么意思,做了一个简单测测试,让后Debug就明白了。代码如下:

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(measureWidth(widthMeasureSpec),measureHeight(heightMeasureSpec));
	}

 

	/**
	 * 高度
	 * @param heightMeasureSpec
	 * @return
	 */
	private int measureHeight(int heightMeasureSpec) {
		int specMode = MeasureSpec.getMode(heightMeasureSpec);
		int specSize = MeasureSpec.getSize(heightMeasureSpec);//父的空间大小
		int h = bitmap.getHeight();		//图片的高度
		int result = 0 ;
		if (specMode == MeasureSpec.EXACTLY) {//父为 wrap_content
			result = Math.min(h, specSize);
		}else if(specMode == MeasureSpec.AT_MOST){//父为fill_parent
 			result = specSize;
		} else {
			result = specSize; 
		}
		return result;
	}

 main.xml (使用两个layout,就是证明,size,不是整个,而是view的父容器的大小)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent" 	//修改该值
    android:layout_height="fill_parent"    //修改该值
    android:id="@+id/layoutId"
     android:layout_weight="2"
    />
    
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layoutId1"
    android:background="#FFFFFFCC"
     android:layout_weight="1"
   />   
    
    
</LinearLayout>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics