Android 探究-----继承ViewGroup后我们可以做什么?
2014-10-12 21:35
381 查看
假设我们用CustomView继承ViewGroup,
在xml文件中,假设CustomView里面的23个childView,那么在生成CustomView的对象时自动回调23次父类ViewGroup的generateLayoutParams(AttributeSet attributeSet)和checkLayoutParams(ViewGroup.LayoutParams p);
上面的规律告诉我们,有多少个childView就调用多少次这两个方法:generateLayoutParams(AttributeSet attributeSet)和checkLayoutParams(ViewGroup.LayoutParams p),通过进一步研究我们发现,generateLayoutParams(AttributeSet attributeSet)方法中的attributeSet就是childView的属性集,所以如果我们要对childView加入自定义属性时,在CustomView中重写generateLayoutParams(AttributeSet
attributeSet) 方法;
上面的LayoutParams类就是CustomView的内部静态类,示例代码:
其中用到的声明自定义属性集的文件attrs.xml代码为:
当给childView加入属性后,如果childView调用getLayoutParams(),一定要记得LayoutParams lp = (LayoutParams) child.getLayoutParams();
而CustomView当通过代码去addView时,如果被add的View不调用setLayoutParams,则回调父类ViewGroup的generateLayoutParams()和checkLayoutParams(ViewGroup.LayoutParams p);如果被add的View调用setLayoutParams,则只回调父类ViewGroup的checkLayoutParams(ViewGroup.LayoutParams
p);
参考:https://github.com/ApmeM/android-flowlayout
在xml文件中,假设CustomView里面的23个childView,那么在生成CustomView的对象时自动回调23次父类ViewGroup的generateLayoutParams(AttributeSet attributeSet)和checkLayoutParams(ViewGroup.LayoutParams p);
上面的规律告诉我们,有多少个childView就调用多少次这两个方法:generateLayoutParams(AttributeSet attributeSet)和checkLayoutParams(ViewGroup.LayoutParams p),通过进一步研究我们发现,generateLayoutParams(AttributeSet attributeSet)方法中的attributeSet就是childView的属性集,所以如果我们要对childView加入自定义属性时,在CustomView中重写generateLayoutParams(AttributeSet
attributeSet) 方法;
@Override public LayoutParams generateLayoutParams(AttributeSet attributeSet) { return new LayoutParams(getContext(), attributeSet); }
上面的LayoutParams类就是CustomView的内部静态类,示例代码:
public static class LayoutParams extends ViewGroup.LayoutParams { private static int NO_SPACING = -1; private int x; private int y; private int horizontalSpacing = NO_SPACING; private int verticalSpacing = NO_SPACING; private boolean newLine = false; public LayoutParams(Context context, AttributeSet attributeSet) { super(context, attributeSet); this.readStyleParameters(context, attributeSet); } public LayoutParams(int width, int height) { super(width, height); } public LayoutParams(ViewGroup.LayoutParams layoutParams) { super(layoutParams); } public boolean horizontalSpacingSpecified() { return horizontalSpacing != NO_SPACING; } public boolean verticalSpacingSpecified() { return verticalSpacing != NO_SPACING; } public void setPosition(int x, int y) { this.x = x; this.y = y; } private void readStyleParameters(Context context, AttributeSet attributeSet) { TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.FlowLayout_LayoutParams); try { horizontalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_LayoutParams_layout_horizontalSpacing, NO_SPACING); verticalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_LayoutParams_layout_verticalSpacing, NO_SPACING); newLine = a.getBoolean(R.styleable.FlowLayout_LayoutParams_layout_newLine, false); } finally { a.recycle(); } } }
其中用到的声明自定义属性集的文件attrs.xml代码为:
<resources> <declare-styleable name="FlowLayout"> <attr name="horizontalSpacing" format="dimension"/> <attr name="verticalSpacing" format="dimension"/> <attr name="orientation" format="enum"> <enum name="horizontal" value="0"/> <enum name="vertical" value="1"/> </attr> <attr name="debugDraw" format="boolean" /> </declare-styleable> <declare-styleable name="FlowLayout_LayoutParams"> <attr name="layout_newLine" format="boolean"/> <attr name="layout_horizontalSpacing" format="dimension"/> <attr name="layout_verticalSpacing" format="dimension"/> </declare-styleable> </resources>
当给childView加入属性后,如果childView调用getLayoutParams(),一定要记得LayoutParams lp = (LayoutParams) child.getLayoutParams();
而CustomView当通过代码去addView时,如果被add的View不调用setLayoutParams,则回调父类ViewGroup的generateLayoutParams()和checkLayoutParams(ViewGroup.LayoutParams p);如果被add的View调用setLayoutParams,则只回调父类ViewGroup的checkLayoutParams(ViewGroup.LayoutParams
p);
参考:https://github.com/ApmeM/android-flowlayout
相关文章推荐
- 探究Android中通过继承ViewGroup自定义控件的原理
- Android 图片轮番 继承ViewGroup实现 可以直接使用
- 探究Android中通过继承ViewGroup自定义控件的原理
- 探究Android中通过继承ViewGroup自定义控件的原理
- 在Android中,可以自定义类,继承ViewGroup等容器类,以实现自己需要的布局显示。
- Android继承ViewGroup研究(1)
- Android 继承ViewGroup研究(3)
- Android 自定义可以滚动的ViewGroup
- android自定义控件系列教程----继承ViewGroup实现带阻力效果的可回弹的SrollView
- [Android实例] 【小木桩】继承viewgroup实现左侧菜单(半原创)
- android自定义控件继承View,其中父类的三个构造方法有什么区别?
- LayoutParams继承于Android.View.ViewGroup.LayoutParams.
- Android自定义View(四)继承ViewGroup
- android 继承viewgroup的类不调用onDraw
- android 自定义View学习总结-继承自ViewGroup
- LayoutParams继承于Android.View.ViewGroup.LayoutParams.
- Android自定义控件之继承ViewGroup创建新容器(四)
- Android_View,ViewGroup,Window之间的关系 android activity,intent,service是什么关系?
- LayoutParams继承于Android.View.ViewGroup.LayoutParams(转)
- android 自定义View学习总结-继承自ViewGroup(2)