viewgroup浅析
来源:互联网 发布:java因式分解 编辑:程序博客网 时间:2024/05/16 08:58
查阅了网上的相关资料,在此做下笔记:
1. measure(并调用子视图的measure)
2. layout(并调用子视图的layout)
3 dispatchDraw(对canvas进行操作(translate),绘制子视图)
下边是我写的一个例子
public class CustomLayout extends View {public CustomLayout(Context context) {super(context);initView();}public CustomLayout(Context context, AttributeSet attrs) {super(context, attrs);initView();}public void initView() {mChildren = new ArrayList<View>();Context context = getContext();Button button = new Button(context); {ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);button.setLayoutParams(params);}button.setText("hello");addView(button);TextView tv = new TextView(context); {ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);tv.setLayoutParams(params);}tv.setText("world");addView(tv);}public void addView(View v) {mChildren.add(v);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int count = mChildren.size();for (int i=0; i < count; i++) {View v = mChildren.get(i);v.measure(v.getLayoutParams().width, v.getLayoutParams().height);}}@Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {int count = mChildren.size();for (int i=0; i < count; i++) {View v = mChildren.get(i);v.layout(left, top, left+v.getMeasuredWidth(), top+v.getMeasuredHeight());top += v.getMeasuredHeight() + 5;}}@Overridepublic void draw(Canvas canvas) {super.draw(canvas);dispatchDraw(canvas);}@Overrideprotected void dispatchDraw(Canvas canvas) {//super.dispatchDraw(canvas);int count = mChildren.size();for (int i=0; i < count; i++) {View v = mChildren.get(i);canvas.save();canvas.translate(v.getLeft(), v.getTop());v.draw(canvas);canvas.restore();}}List<View> mChildren;}
效果图:
- viewgroup浅析
- android viewgroup浅析
- 浅析ViewGroup中的MeasureSpec和LayoutParams
- 3.1.1 View与ViewGroup浅析
- Android Activity和ViewGroup中的dispatchTouchEvent机制浅析
- ViewGroup
- ViewGroup
- ViewGroup
- ViewGroup
- ViewGroup
- ViewGroup
- ViewGroup
- ViewGroup
- VIEWGROUP
- viewGroup
- Android-浅析自定义ViewGroup(附一个子控件根据父控件行宽自动换行的LineWrapLayout 案例)
- Android ViewGroup
- ViewGroup 学习
- 红尘恋歌
- 自动构建工具Ant的使用(二)
- static_cast、dynamic_cast、reinterpret_cast和const_cast
- hibernate连接mysql示范
- ACM小技巧
- viewgroup浅析
- 排列问题
- <每日一句英语> 2012-9-25
- 邂逅-------秋天的童话
- 中断四
- org.hibernate.id.IdentifierGenerationException:我的第一个hibernate异常
- 在servlet和非servlet实体类中如何读取资源文件
- ie8 window.open 打开窗口带滚动条
- Linux下多进程/多线程编程