Android的 - 做一个ViewGroup的onDraw需要通过子视图进行迭代,并明确调用的onDraw? [英] Android - do ViewGroup onDraw need to iterate through child view and call onDraw explicitly?

查看:247
本文介绍了Android的 - 做一个ViewGroup的onDraw需要通过子视图进行迭代,并明确调用的onDraw?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了整整一天的调试各种方式来定制的ViewGroup 添加到另一个定制的ViewGroup ,几乎疯了因为他们没有工作,还有就是说明它是如何做到没有官方的文件或样品...

I have spent the whole day debugging various ways to add custom ViewGroup into another custom ViewGroup and nearly went crazy because none of them works, and there is no official documentation or sample that shows how it can be done...

基本上,我有2个自定义的的ViewGroup

Basically, I have 2 custom ViewGroup:


  1. Horizo​​ntalDockView 扩展的ViewGroup

  2. GameEntryView 扩展的FrameLayout

  1. HorizontalDockView extends ViewGroup
  2. GameEntryView extends FrameLayout

Horizo​​ntalDockView 覆盖的onDraw onMeasure 等,并一切都通常被称为和完美的作品。
但是,当我创建 GameEntryView Horizo​​ntalDockView 的构造函数中,并调用 addView(gameEntryView) gameEntryView 永远不会显示无论的LayoutParams addView 无论从任何线程调用的,但是还是我打电话,负载,并在家长的setContentView Horizo​​ntalDockView 。如果我通过列出 horizo​​ntalDockView.getChildAt(); 所有的 gameEntryView 对象仍然存在。

HorizontalDockView overrides onDraw, onMeasure, etc and everything is called normally and works perfectly. However, when I create GameEntryView from inside HorizontalDockView's constructor and call addView(gameEntryView), the gameEntryView will never ever show regardless of the layoutParams, addView called from whatever thread, or however I call, load, and setContentView on the parent HorizontalDockView. If I list through the horizontalDockView.getChildAt(); all the gameEntryView objects are still there.

无望,我试图通过GameEntryView的的onDraw onMeasure dispatchDraw 的方法和他们没有意识到真正被调用!号。一次也没有!

Hopeless, I try to debug through GameEntryView's onDraw, onMeasure, dispatchDraw methods and realized none of them actually get called! No.. not even once!

我需要通过在*通话和家长(Horizo​​ntalDockView的)所有的子视图迭代叫孩子们对*明确?我只是父调用super.on *()。

Do I need to iterate through all the child view in the parent (HorizontalDockView's) on* call and call the children's on* explicitly? I was just calling super.on*() on the parent.

我打过电话 setWillNotDraw(假); 父和子类都

我如何让孩子露面父视图内?简单的样品或现有的小开源项目是高度AP preciated!

How do I get the child to show up inside the parent's view? simple sample or existing small open source project is highly appreciated!

非常感谢你!

推荐答案

你覆盖 onLayout ?当Android的勾画出你的的ViewGroup ,您的ViewGroup 负责铺设的孩子们。

Did you overwrite onLayout? When Android lays out your ViewGroup, your ViewGroup is responsible for laying out the children.

这code是,规定了对彼此顶部所有儿童定制的ViewGroup:

This code is from a custom ViewGroup that lays out all children on top of each other:

@Override
protected void onLayout(final boolean changed, final int l, final int t, final int r, final int b) {

    int count = this.getChildCount();
    for (int i = 0; i < count; i++) {

        View child = this.getChildAt(i);
        child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight());
    }
}

为了完整起见, onMeasure 覆盖:

@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {

    int parentWidth  = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
    this.setMeasuredDimension(parentWidth, parentHeight);

    int count = this.getChildCount();
    for (int i = 0; i < count; i++) {

        View child = this.getChildAt(i);
        this.measureChild(
            child,
            MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(parentHeight, MeasureSpec.EXACTLY));
    }
}

这篇关于Android的 - 做一个ViewGroup的onDraw需要通过子视图进行迭代,并明确调用的onDraw?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆