首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ViewGroup误差

ViewGroup误差
EN

Stack Overflow用户
提问于 2015-04-06 09:19:47
回答 1查看 52关注 0票数 0

因此,作为实践,我正在学习如何编程地创建视图。我创建了一个新的布局扩展视图组(我称之为Custom1),它将子视图(大小相同)放在两列中。

这个组的子组也是一个自定义布局(我称之为Custom2),包含一个图像视图和两个文本视图。我使用for循环将必要数量的视图添加到视图组中,onLayout就会被覆盖。

现在,我尝试在Nexus 4上运行这个选项,选中了“显示布局边界”选项。我可以看到Custom1的子级的边界都在正确的位置,并且基于日志,custom2的子级也在正确的位置。但是,只有第一个" custom2“正在正确显示(即第一个custom2显示一个imageView和两个文本视图,其余为空)。

父视图是否可能掩盖子视图?

如果没有,以前有没有人遇到过类似的问题?

下面是我为custom1编写的一些代码:

代码语言:javascript
复制
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();
    if (ItemToDebug.equals("Layout")){
        if (count == numberOfChannels){
            Log.d("Layout:", "Number of children matches number of channels");
        }else{
            Log.d("Layout:", "Mismatch between number of children and number of channels");
        }
        Log.d("Layout:", "onLayout " + Integer.toString(count) + " children");
    }

    for (int i = 0; i < count; i++){
        View child = getChildAt(i);
        if (i%2 == 0){
            if (LayoutLeftChild(i/2, l, t, r, b, child)){ //Lays out Child, returning a Boolean if successful
                if (ItemToDebug.equals("Layout")){
                    Log.d("Layout:", "onLayoutLeftChild number " + Integer.toString(i/2) + "successful");
                }
            } else
            if (ItemToDebug.equals("Layout")){
                Log.d("Layout:", "onLayoutLeftChild number " + Integer.toString(i/2) + "failed");
            }
        }
        if (i%2 == 1){
            if (LayoutRightChild(i/2, l, t, r, b, child)){
                if (ItemToDebug.equals("Layout")){
                    Log.d("Layout:", "onLayoutRightChild number " + Integer.toString(i/2) + "successful");
                }
            }else{
                if (ItemToDebug.equals("Layout")){
                    Log.d("Layout:", "onLayoutRightChild number " + Integer.toString(i/2) + "failed");
                }
            }
        }
    }

}


/*
Left edge for right column = l + (r-l)/2 + 15
Right edge for right column = r - 20
20dp Margin between rows
Rows are 400 dp tall
 */
private boolean LayoutRightChild(int i, int l, int t, int r, int b, View child) {
    final View Child = child;

    final int Left = l + (r-l)/2 + 15;
    final int Right = r - 20;
    final int Top = t + i*20 + (i-1)*400;
    final int Bottom = Top + 400;

    Child.layout(Left, Top, Right, Bottom);
    if (ItemToDebug.equals("Layout")){
        Log.d("Layout:", "Child laid out at (" + Integer.toString(Left) + ", " + Integer.toString(Top) + ", " + Integer.toString(Right) + ", " + Integer.toString(Bottom) + ")");
    }

    return true;
}



/*
Left edge for left column = l + 20
Right edge for left column = l + (r-l)/2 - 15
20dp Margin between rows
Rows are 400 dp tall
 */
private boolean LayoutLeftChild(int i, int l, int t, int r, int b, View child) {
    final View Child = child;

    final int Left = l + 20;
    final int Right = l + (r-l)/2 - 15;
    final int Top = t + i*20 + (i-1)*400;
    final int Bottom = Top + 400;

    Child.layout(Left, Top, Right, Bottom);

    if (ItemToDebug.equals("Layout")){
        Log.d("Layout:", "Child laid out at (" + Integer.toString(Left) + ", " + Integer.toString(Top) + ", " + Integer.toString(Right) + ", " + Integer.toString(Bottom) + ")");
    }
    return true;
}

下面是来自custom2的一些代码:

代码语言:javascript
复制
 @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {

    final int count = getChildCount(); //There should be three children - one ImageView on top and two TextViews on bottom
    if (ItemToDebug.equals("Layout")){
        if (count == 3){
            Log.d("View Contents:", "3 Children Views found");
        }else{
            Log.d("View Contents:", "Number of Children Incorrect. " + Integer.toString(count) + " children found.");
        }
        Log.d("Layout:", "onLayout " + Integer.toString(count) + " children");
    }

    //Get children here in for loop and place.
    for (int i = 0; i < count; i++){
        final View child = this.getChildAt(i);

        //Layout should already have margins, so align contents with left and right sides.
        int width = r - l;
        int top;
        int height;
        switch(i){
            case 0:
                top = t;
                height = 100;
                if (ItemToDebug.equals("Layout")) {
                    Log.d("Layout:", "Image Laid out at (" + Integer.toString(l) + ", " + Integer.toString(top) + ", " + Integer.toString(r) + ", " + Integer.toString(top + height) + ")");
                }
                break;
            case 1:
                top = t + 100;
                height = 60;
                if (ItemToDebug.equals("Layout")) {
                    Log.d("Layout:", "TextView (nowPlaying) Laid out at (" + Integer.toString(l) + ", " + Integer.toString(top) + ", " + Integer.toString(r) + ", " + Integer.toString(top + height) + ")");
                }
                break;
            case 2:
                top = t + 160;
                height = 60;
                if (ItemToDebug.equals("Layout")) {
                    Log.d("Layout:", "TextView (nextPlaying) Laid Out at (" + Integer.toString(l) + ", " + Integer.toString(top) + ", " + Integer.toString(r) + ", " + Integer.toString(top + height) + ")");
                }
                break;
            default:
                top = t;
                height = 0;
                if (ItemToDebug.equals("Layout")){
                    Log.d("Layout:", "More than 3 children have been added to the Custom2");
                }
                break;
        }

        child.layout(l, top, r, top + height);

    }
}

这是我的日志:

9jazWEZ1RXh4a2VXdnM/view?usp=sharing

还有截图。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-06 09:52:45

答案可以在这里找到:Views within a Custom ViewGroup are not showing

孩子们被安置在我们的wrt上他们的父母(左边和顶部是0)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29468442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档