标签:android style blog http io ar os 使用 sp
测试结果如下:
* wrap_parent -> MeasureSpec.AT_MOST
* match_parent -> MeasureSpec.EXACTLY
* 具体值 -> MeasureSpec.EXACTLY
一个 view 的 onMeasure 方法最终得到的测量规格值(测量约束值)中包含的测量模式和上面不一定对的上,这是因为 onMeasure 方法中得到的测量规格值(测量约束值)是 measure 方法传过来的,父 view 在调用 measure 方法的时候可以根据自己的情况不使用 ViewGroup 提供的 measureChildren 方法,而改变上面的映射关系,比如 RelativeLayout 中就结合其他 Layout 属性综合确定 MeasureSpec 中的模式。上面的映射关系可以直接查看 ViewGroup 类的 measureChildren 方法,这是最简单的 measure 方式.
ViewGroup 中 measureChild 方法如下:
protected void measureChild(View child, int parentWidthMeasureSpec,
int parentHeightMeasureSpec) {
final LayoutParams lp = child.getLayoutParams();
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom, lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}测试代码如下
TestView.java
/**
* 测试 match_parent、wrap_parent、具体值 和 MeasureSpec 中 mode 的对应关系
*/
package com.example.testmeasurespec;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class TestView extends View {
private static final String TAG = "TestView";
public TestView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public TestView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TestView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.i(TAG, "onMeasure:" + getTag());
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
public static int getDefaultSize(int size, int measureSpec) {
int result = size;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
switch (specMode) {
case MeasureSpec.UNSPECIFIED:
Log.i(TAG, "specMode:MeasureSpec.UNSPECIFIED");
result = size;
break;
case MeasureSpec.AT_MOST:
Log.i(TAG, "specMode:MeasureSpec.AT_MOST");
case MeasureSpec.EXACTLY:
Log.i(TAG, "specMode:MeasureSpec.EXACTLY");
result = specSize;
break;
}
return result;
}
}
public class TestLayout extends ViewGroup {
public TestLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TestLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TestLayout(Context context) {
super(context);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
measureChildren(widthMeasureSpec, heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}<com.example.testmeasurespec.TestLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.testmeasurespec.TestView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
android:tag="test1" />
<com.example.testmeasurespec.TestView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_launcher"
android:tag="test2" />
<com.example.testmeasurespec.TestView
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/ic_launcher"
android:tag="test3" />
</com.example.testmeasurespec.TestLayout>match_parent、wrap_parent、具体值 和 MeasureSpec 类中 mode 的对应关系
标签:android style blog http io ar os 使用 sp
原文地址:http://blog.csdn.net/zhaoyw2008/article/details/41355819