Android View measure (五) 支持margin属性,从一个异常说起
2016-08-27 15:04
405 查看
先来看下代码
一、查看夏目
1. 自定义控件
[java]
view plain
copy
print?
public class CustomViewGroup extends ViewGroup {
......
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 遍历所有子视图,进行measure操作
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child != null && child.getVisibility() != View.GONE) {
measureChild(child, widthMeasureSpec, heightMeasureSpec);
// 支持子视图设置的android:layout_margin属性
MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayoutParams();
int marginLeft = layoutParams.leftMargin;
}
}
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
for (int i = 0; i < getChildCount(); i++) {
View childView = getChildAt(i);
// 只为最简单代码复现BUG,所有子视图都随便放
childView.layout(left, top, left + childView.getMeasuredWidth(), top + childView.getMeasuredHeight());
}
}
}
继承自ViewGroup,主要是演示自定义视图中如何支持margin属性,重点在child.getLayoutParams()一行,接下来看下布局文件中如何使用
2. 布局文件
[java]
view plain
copy
print?
<com.example.android.apis.CustomViewGroup
android:id="@+id/custom_view_group"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
oid:layout_margin="10dip"
android:text="love_world_" />
</com.example.android.apis.CustomViewGroup>
重头戏:android:layout_margin="10dip" margin属性
3. 异常
[plain]
view plain
copy
print?
java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams
at com.example.android.apis.CustomViewGroup.onMeasure(CustomViewGroup.java:50)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16831)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2586)
at android.view.View.measure(View.java:16831)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2189)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1352)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1535)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:561)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)
出现以上异常的原因,LayoutParams从哪里来的?
视图的加载有两种方式一种是代码addView 一种是inflate 。
1. inflate 方法加载并添加LayoutParams
Android中measure过程、WRAP_CONTENT详解以及xml布局文件解析流程浅析(上)
2. 以下演示addView方式添加LayoutParams
[java]
view plain
copy
print?
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomViewGroup customViewGroup = (CustomViewGroup) findViewById(R.id.custom_view_group);
customViewGroup.addView(createTextView("Love_world_"));
}
private View createTextView(String value) {
TextView textView = new TextView(this);
textView.setText("a child view");
textView.setText(value);
textView.setLayoutParams(new ViewGroup.MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return textView;
}
}
2. ViewGroup.addView源码,查找何处添加LayoutParams
[java]
view plain
copy
print?
public abstract class ViewGroup extends View implements ViewParent, ViewManager {
public void addView(View child) {
addView(child, -1);
}
public void addView(View child, int index) {
LayoutParams params = child.getLayoutParams();
// 子视图LayoutParams为为空是处理方式
if (params == null) {
params = generateDefaultLayoutParams();
if (params == null) {
throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
}
}
addView(child, index, params);
}
public void addView(View child, int width, int height) {
final LayoutParams params = generateDefaultLayoutParams();
params.width = width;
params.height = height;
addView(child, -1, params);
}
public void addView(View child, LayoutParams params) {
addView(child, -1, params);
}
public void addView(View child, int index, LayoutParams params) {
if (DBG) {
System.out.println(this + " addView");
}
// addViewInner() will call child.requestLayout() when setting the new LayoutParams
// therefore, we call requestLayout() on ourselves before, so that the child's request
// will be blocked at our level
requestLayout();
invalidate();
addViewInner(child, index, params, false);
}
// 子视图默认LayoutParams实例
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
// 定义LayoutParams类
public static class LayoutParams {
public int width;
public int height;
public LayoutParams(int width, int height) {
this.width = width;
this.height = height;
}
}
}
以下是关键
[java]
view plain
copy
print?
public abstract class ViewGroup extends View implements ViewParent, ViewManager {
private void addViewInner(View child, int index, LayoutParams params,
boolean preventRequestLayout) {
if (child.getParent() != null) {
throw new IllegalStateException("The specified child already has a parent. " +
"You must call removeView() on the child's parent first.");
}
if (!checkLayoutParams(params)) {
params = generateLayoutParams(params);
}
if (preventRequestLayout) {
child.mLayoutParams = params;
} else {
child.setLayoutParams(params);
}
......
}
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p != null;
}
}
通过查看以上addView添加LayoutParams代码可以发现解决方案,复写这些函数,创建当前自定义视图的LayoutParams继承自MarginLayoutParams。
[java]
view plain
copy
print?
public class CustomViewGroup extends ViewGroup {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
......
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
......
}
@Override
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p);
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
// 继承自margin,支持子视图android:layout_margin属性
public static class LayoutParams extends MarginLayoutParams {
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
}
public LayoutParams(int width, int height) {
super(width, height);
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
public LayoutParams(ViewGroup.MarginLayoutParams source) {
super(source);
}
}
}
一、查看夏目
1. 自定义控件
[java]
view plain
copy
print?
public class CustomViewGroup extends ViewGroup {
......
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 遍历所有子视图,进行measure操作
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child != null && child.getVisibility() != View.GONE) {
measureChild(child, widthMeasureSpec, heightMeasureSpec);
// 支持子视图设置的android:layout_margin属性
MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayoutParams();
int marginLeft = layoutParams.leftMargin;
}
}
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
for (int i = 0; i < getChildCount(); i++) {
View childView = getChildAt(i);
// 只为最简单代码复现BUG,所有子视图都随便放
childView.layout(left, top, left + childView.getMeasuredWidth(), top + childView.getMeasuredHeight());
}
}
}
继承自ViewGroup,主要是演示自定义视图中如何支持margin属性,重点在child.getLayoutParams()一行,接下来看下布局文件中如何使用
2. 布局文件
[java]
view plain
copy
print?
<com.example.android.apis.CustomViewGroup
android:id="@+id/custom_view_group"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
oid:layout_margin="10dip"
android:text="love_world_" />
</com.example.android.apis.CustomViewGroup>
重头戏:android:layout_margin="10dip" margin属性
3. 异常
[plain]
view plain
copy
print?
java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams
at com.example.android.apis.CustomViewGroup.onMeasure(CustomViewGroup.java:50)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16831)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2586)
at android.view.View.measure(View.java:16831)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2189)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1352)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1535)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:561)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)
出现以上异常的原因,LayoutParams从哪里来的?
视图的加载有两种方式一种是代码addView 一种是inflate 。
1. inflate 方法加载并添加LayoutParams
Android中measure过程、WRAP_CONTENT详解以及xml布局文件解析流程浅析(上)
2. 以下演示addView方式添加LayoutParams
[java]
view plain
copy
print?
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomViewGroup customViewGroup = (CustomViewGroup) findViewById(R.id.custom_view_group);
customViewGroup.addView(createTextView("Love_world_"));
}
private View createTextView(String value) {
TextView textView = new TextView(this);
textView.setText("a child view");
textView.setText(value);
textView.setLayoutParams(new ViewGroup.MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return textView;
}
}
2. ViewGroup.addView源码,查找何处添加LayoutParams
[java]
view plain
copy
print?
public abstract class ViewGroup extends View implements ViewParent, ViewManager {
public void addView(View child) {
addView(child, -1);
}
public void addView(View child, int index) {
LayoutParams params = child.getLayoutParams();
// 子视图LayoutParams为为空是处理方式
if (params == null) {
params = generateDefaultLayoutParams();
if (params == null) {
throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
}
}
addView(child, index, params);
}
public void addView(View child, int width, int height) {
final LayoutParams params = generateDefaultLayoutParams();
params.width = width;
params.height = height;
addView(child, -1, params);
}
public void addView(View child, LayoutParams params) {
addView(child, -1, params);
}
public void addView(View child, int index, LayoutParams params) {
if (DBG) {
System.out.println(this + " addView");
}
// addViewInner() will call child.requestLayout() when setting the new LayoutParams
// therefore, we call requestLayout() on ourselves before, so that the child's request
// will be blocked at our level
requestLayout();
invalidate();
addViewInner(child, index, params, false);
}
// 子视图默认LayoutParams实例
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
// 定义LayoutParams类
public static class LayoutParams {
public int width;
public int height;
public LayoutParams(int width, int height) {
this.width = width;
this.height = height;
}
}
}
以下是关键
[java]
view plain
copy
print?
public abstract class ViewGroup extends View implements ViewParent, ViewManager {
private void addViewInner(View child, int index, LayoutParams params,
boolean preventRequestLayout) {
if (child.getParent() != null) {
throw new IllegalStateException("The specified child already has a parent. " +
"You must call removeView() on the child's parent first.");
}
if (!checkLayoutParams(params)) {
params = generateLayoutParams(params);
}
if (preventRequestLayout) {
child.mLayoutParams = params;
} else {
child.setLayoutParams(params);
}
......
}
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p != null;
}
}
通过查看以上addView添加LayoutParams代码可以发现解决方案,复写这些函数,创建当前自定义视图的LayoutParams继承自MarginLayoutParams。
[java]
view plain
copy
print?
public class CustomViewGroup extends ViewGroup {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
......
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
......
}
@Override
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p);
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
// 继承自margin,支持子视图android:layout_margin属性
public static class LayoutParams extends MarginLayoutParams {
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
}
public LayoutParams(int width, int height) {
super(width, height);
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
public LayoutParams(ViewGroup.MarginLayoutParams source) {
super(source);
}
}
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories