在自定义视图的ViewGroup不显示 [英] Views within a Custom ViewGroup are not showing

查看:486
本文介绍了在自定义视图的ViewGroup不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近钻研创建自定义ViewGroups也碰到一个问题,我想不通。

我有2 ViewGroups - ViewManager和文章

ViewManager只是勾画出低于previous文章的文章(即像垂直的LinearLayout)

第二十安排几个TextViews和ImageView的,你可以在图像中看到的。创建一个单独的文章并将它添加到ViewManager工作正常,一切都显示出来,但是当我添加第二个文章没有任何文章的内容是可见的。

那么,为什么没有一个TextViews或ImageView的显示出来了(注意蓝色条是绘制canvas.drawRect()中的OnDraw())。我也打印出来(通过logcat的)儿童享有的约束值(即getLeft()/顶()/右()在drawChild /下()(),他们似乎都已经很好了。

 第一的TextView
先左后右5 225顶26底147
FIRST的TextView
先左后右5 320 147顶底198
FIRST的TextView
先左后右5 180顶208底部222
第一ImageView的
先左后右225 315顶34底129
第二次的TextView
左二10右53顶238底部257
第二次的TextView
左二右5 325 257顶底349
第二次的TextView
左二右5 320 349顶底400
第二次的TextView
左二右5 180顶410底部424
 

因此​​,没有人有什么我做错的想法?​​

在第onMeasure方法

 保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
    INT widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
    INT widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);

    INT heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
    INT heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
    specWidth = widthSpecSize;
    INT totalHeight = 0;

    INT宽度= 0;
    如果(mImage = NULL和放大器;!&安培;!mImage.getParent()= NULL){
        measureChild(mImage,MeasureSpec.makeMeasureSpec(100,MeasureSpec.AT_MOST),MeasureSpec.makeMeasureSpec(100,MeasureSpec.AT_MOST));
        宽度= widthSpecSize-mImage.getWidth();
        ImageWidth等= mImage.getMeasuredWidth();
    }
    的for(int i = 0; I< this.getChildCount();我++){
        最后查看孩子= this.getChildAt(我);

        //获取可用视图的宽度减去图像宽度
        如果(ImageWidth等大于0)
            宽度= widthSpecSize- ImageWidth等;
        其他
            宽度= widthSpecSize;

        //只测量textviews
        如果(!(孩子的instanceof ImageView的)){
            measureChild(儿童,MeasureSpec.makeMeasureSpec(宽度,MeasureSpec.AT_MOST),heightMeasureSpec);
            //计算的观点总高度上,用于设置的维
            totalHeight + = child.getMeasuredHeight();
        }
    }
    //如果标题高度大于所述图像则片断
    //可拉伸至全宽
    如果(mTitle.getMeasuredHeight()> mImage.getMeasuredHeight())
        measureChild(mSnippet,MeasureSpec.makeMeasureSpec(widthSpecSize,MeasureSpec.AT_MOST),heightMeasureSpec);

    //措施源,使其全宽
    measureChild(mSource,MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT,MeasureSpec.AT_MOST),heightMeasureSpec);
    setMeasuredDimension(widthSpecSize,totalHeight);
}
 

和onLayout方法

 保护无效onLayout(布尔改,诠释离开,INT顶部,诠释权,诠释底部){

    INT newLeft =左+ outerMargin;
    INT newTop =顶部+ marginTop;
    INT prevHeight = 0;

    如果(mEngine!= NULL){

        INT高= mEngine.getMeasuredHeight();
        INT childRight = newLeft + mEngine.getMeasuredWidth();
        mEngine.layout(newLeft + marginLeft,newTop + prevHeight + 2,childRight + marginLeft,newTop +高度+ prevHeight + 2);
        maxRight = Math.max(maxRight,childRight);
        prevHeight + =身高+ 2;
        topBarHeight = mEngine.getMeasuredHeight()+(marginLeft * 2);
        maxBottom = Math.max(maxBottom,mEngine.getBottom());
    }
    如果(mTitle!= NULL){
        INT高= mTitle.getMeasuredHeight();
        INT childRight = newLeft + mTitle.getMeasuredWidth();
        mTitle.layout(newLeft,newTop + prevHeight,childRight,newTop +高度+ prevHeight);
        maxRight = Math.max(maxRight,childRight);
        prevHeight + =高度;
        maxBottom = Math.max(maxBottom,mTitle.getBottom());
    }
    如果(mSnippet!= NULL){
        INT高= mSnippet.getMeasuredHeight();
        INT childRight = newLeft + mSnippet.getMeasuredWidth();
        mSnippet.layout(newLeft,newTop + prevHeight,右,newTop +高度+ prevHeight);
        maxRight = Math.max(maxRight,childRight);
        prevHeight + =高度;
        maxBottom = Math.max(maxBottom,mSnippet.getBottom());
    }
    如果(mSource!= NULL){
        INT高= mSource.getMeasuredHeight();
        INT childRight = newLeft + mSource.getMeasuredWidth();
        mSource.layout(newLeft,newTop + prevHeight +(marginTop * 2),childRight,newTop +高度+ prevHeight +(marginTop * 2));
        maxRight = Math.max(maxRight,childRight);
        prevHeight + =高度;
        maxBottom = Math.max(maxBottom,mSource.getBottom());

    }
    如果(mImage!= NULL){
        INT高= mImage.getMeasuredHeight();
        日志(MXW+ maxRight);
        INT childRight = maxRight + mImage.getMeasuredWidth();
        mImage.layout(右mImage.getMeasuredWidth()+ 5,newTop + topBarHeight,右5,身高+ topBarHeight);
        totalWidth = childRight;
        maxBottom = Math.max(maxBottom,mImage.getBottom());
    }其他
        totalWidth = maxRight;

}
 

在一个侧面说明是好的使用LayoutParams.WRAP_CONTENT作为makeMeasureSpec()这样的参数?

  MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT,MeasureSpec.AT_MOST)
 

解决方案

onLayout ,孩子们应定位相对于他们的父母。您添加(和离开)给孩子们的坐标。这不是第一产品的一个问题,因为离开为零。对于第二条,离开仍然是零,但的第二篇文章,其ViewManager的顶部。刚刚摆脱 newTop newLeft 和使用,分别是 marginTop outerMargin 在自己的位置。

关于你的一边:第一个参数 makeMeasureSpec 应该是一个维度。通过使用WRAP_CONTENT,你的最大尺寸设置为-2,这可能不是你想要做什么。

I have recently delved into creating custom ViewGroups and have encountered a problem I can't figure out.

I have 2 ViewGroups - ViewManager and Article

ViewManager simply lays out an Article below the previous Article (ie like a vertical LinearLayout)

Article arranges several TextViews and an ImageView as you can see in the image. Creating a single Article and adding it to ViewManager works fine and everything shows up but when I add a second Article none of the content of the Article is visible.

So why are none of the TextViews or the ImageView showing up (note the blue bar is drawn with canvas.drawRect() in onDraw()). I have also printed out (via logcat) the the bound values of the child views(ie getLeft()/Top()/Right()/Bottom() in drawChild() and they all seem to look fine

FIRST TextView
FIRST left 5 right 225 top 26 bottom 147
FIRST TextView
FIRST left 5 right 320 top 147 bottom 198
FIRST TextView
FIRST left 5 right 180 top 208 bottom 222
FIRST ImageView
FIRST left 225 right 315 top 34 bottom 129
SECOND TextView
SECOND left 10 right 53 top 238 bottom 257
SECOND TextView
SECOND left 5 right 325 top 257 bottom 349
SECOND TextView
SECOND left 5 right 320 top 349 bottom 400
SECOND TextView
SECOND left 5 right 180 top 410 bottom 424

So does anyone have an idea of what I've done wrong?

the Article onMeasure method

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSpecSize =  MeasureSpec.getSize(widthMeasureSpec);

    int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);
    specWidth = widthSpecSize;
    int totalHeight=0;

    int width = 0;
    if(mImage!=null&&mImage.getParent()!=null){
        measureChild(mImage,MeasureSpec.makeMeasureSpec(100, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(100, MeasureSpec.AT_MOST));
        width=widthSpecSize-mImage.getWidth();
        imageWidth = mImage.getMeasuredWidth();
    }
    for(int i = 0;i<this.getChildCount();i++){
        final View child = this.getChildAt(i);

        //get the width of the available view minus the image width
        if(imageWidth > 0)
            width =widthSpecSize- imageWidth; 
        else
            width = widthSpecSize;

        //measure only the textviews
        if(!(child instanceof ImageView)){
            measureChild(child, MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), heightMeasureSpec);
            //calculate total height of the views, used to set the dimension
            totalHeight+=child.getMeasuredHeight();
        }
    }
    //if the title height is greater than the image then the snippet
    //can stretch to full width
    if(mTitle.getMeasuredHeight()>mImage.getMeasuredHeight())
        measureChild(mSnippet, MeasureSpec.makeMeasureSpec(widthSpecSize, MeasureSpec.AT_MOST), heightMeasureSpec);

    //measure source to make it full width
    measureChild(mSource,MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, MeasureSpec.AT_MOST), heightMeasureSpec);
    setMeasuredDimension(widthSpecSize, totalHeight);
}

and the onLayout method

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

    int newLeft = left+outerMargin;
    int newTop = top+marginTop;
    int prevHeight = 0;

    if(mEngine!=null){

        int height = mEngine.getMeasuredHeight();
        int childRight = newLeft+mEngine.getMeasuredWidth(); 
        mEngine.layout(newLeft+marginLeft, newTop+prevHeight+2, childRight+marginLeft, newTop+height+prevHeight+2);
        maxRight = Math.max(maxRight, childRight);
        prevHeight += height+2;
        topBarHeight = mEngine.getMeasuredHeight()+(marginLeft*2);
        maxBottom = Math.max(maxBottom, mEngine.getBottom());
    }
    if(mTitle!=null){
        int height = mTitle.getMeasuredHeight();
        int childRight = newLeft+mTitle.getMeasuredWidth();
        mTitle.layout(newLeft, newTop+prevHeight, childRight, newTop+height+prevHeight);
        maxRight = Math.max(maxRight, childRight);
        prevHeight += height;
        maxBottom = Math.max(maxBottom, mTitle.getBottom());
    }
    if(mSnippet!=null){
        int height = mSnippet.getMeasuredHeight();
        int childRight = newLeft+mSnippet.getMeasuredWidth();
        mSnippet.layout(newLeft, newTop+prevHeight, right, newTop+height+prevHeight);
        maxRight = Math.max(maxRight, childRight);
        prevHeight += height;
        maxBottom = Math.max(maxBottom, mSnippet.getBottom());
    }
    if(mSource !=null){
        int height = mSource.getMeasuredHeight();
        int childRight = newLeft+mSource.getMeasuredWidth();
        mSource.layout(newLeft, newTop+prevHeight+(marginTop*2), childRight, newTop+height+prevHeight+(marginTop*2));
        maxRight = Math.max(maxRight, childRight);
        prevHeight += height;
        maxBottom = Math.max(maxBottom, mSource.getBottom());

    }
    if(mImage!=null){
        int height = mImage.getMeasuredHeight();
        log("mxW "+maxRight);
        int childRight = maxRight+mImage.getMeasuredWidth();
        mImage.layout(right-mImage.getMeasuredWidth()+5, newTop+topBarHeight, right-5, height+topBarHeight);
        totalWidth = childRight;
        maxBottom = Math.max(maxBottom, mImage.getBottom());
    }else
        totalWidth = maxRight;

}

On a side note is it okay to use LayoutParams.WRAP_CONTENT as a parameter for makeMeasureSpec() like this?

MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, MeasureSpec.AT_MOST)

解决方案

In onLayout, the children should be positioned with respect to their parent. You are adding top (and left) to the coordinates of the children. That's not a problem for the first Article because top and left are zero. For the second Article, left is still zero but top is the top of the second article in its ViewManager. Just get rid of newTop and newLeft and use, respectively, marginTop and outerMargin in their place.

Regarding your aside: the first argument to makeMeasureSpec is supposed to be a dimension. By using WRAP_CONTENT, you are setting the maximum dimension to -2, which is probably not what you want to do.

这篇关于在自定义视图的ViewGroup不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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