Android-如何在onLayout中设置子视图的位置

Android-如何在onLayout中设置子视图的位置,android,layout,Android,Layout,如何在Android中设置子视图的位置 我已经设法在onLayout()中设置了一点,但是我只能将其设置为某个值,在该值之后,子视图将不会显示。我认为它正在被裁剪,但我不明白为什么会被裁剪,以及如何正确地裁剪 图为: 我设法将糖果粉碎图标向右移动了10,如果我再这样做,所有的图标都不会显示…:( 代码如下: @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super

如何在Android中设置子视图的位置

我已经设法在onLayout()中设置了一点,但是我只能将其设置为某个值,在该值之后,子视图将不会显示。我认为它正在被裁剪,但我不明白为什么会被裁剪,以及如何正确地裁剪

图为:

我设法将糖果粉碎图标向右移动了10,如果我再这样做,所有的图标都不会显示…:(

代码如下:

    @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    // Do nothing. Do not call the superclass method--that would start a layout pass
    // on this view's children. PieChart lays out its children in onSizeChanged().
//      super.onLayout(changed, l, t, r, b);
    Log.e(LOG_TAG, LOG_TAG + ".onLayout: " + this + ": "+ l + ", " + t + ", " + r + ", " + b);

//      // this is successful
//      RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) this.getLayoutParams();//new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT );
//      lp.setMargins( lp.leftMargin + 5, lp.topMargin + 5, lp.rightMargin + 5, lp.bottomMargin + 5);
//      setLayoutParams( lp );
//      this.requestLayout();

    int iChildCount = this.getChildCount();
    for ( int i = 0; i < iChildCount; i++ ) {
        int iLeft = i * getIconSize(); // cannot be more than 10, otherwise nothing will show
        View pChild = this.getChildAt(i);
//          Log.d(LOG_TAG, LOG_TAG + ".onLayout child: " + pChild + " size: " + l + ", " + t + ", " + r + ", " + b);
        Log.d(LOG_TAG, LOG_TAG + ".onLayout child: " + pChild + " size: " + pChild.getMeasuredWidth() + ", " + pChild.getMeasuredHeight() + " :: " + pChild.getWidth() + ", " + pChild.getHeight());
        Log.d(LOG_TAG, LOG_TAG + ".onLayout child: " + pChild + " boundary: " + pChild.getLeft() + ", " + pChild.getTop() + ", " + pChild.getRight() + ", " + pChild.getBottom());

        pChild.layout(iLeft, 0, iLeft + pChild.getMeasuredWidth(), pChild.getMeasuredHeight());
//          pChild.layout(l, t, pChild.getMeasuredWidth(), pChild.getMeasuredHeight());

//          LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) pChild.getLayoutParams();
//          lp.setMargins(iLeft, 0, 0, 0);
//          pChild.setLayoutParams(lp);
//          pChild.requestLayout();
    }
}
编辑:

澄清答案


正如@Ben75所指出的,问题是由于错误地设置了pChild.layout(iTop、iLeft、iRight、iBottom);值造成的。但是,pChild.layout不是视图的onLayout调用的,而是父级的onLayout调用的。父级的onLayout遍历每个子级并调用它们的布局(iTop、iLeft、iRight、iBottom);功能也很好,因为它将儿童的布局设置为(0,0、iWidth、iHeight),因此会发生剪辑

我想问题出在这里:

pChild.layout(iLeft, 0, pChild.getMeasuredWidth(), pChild.getMeasuredHeight());
第三个和第四个arg是相对于父对象的右下距离。请尝试以下操作:

pChild.layout(iLeft, 0, getMeasuredWidth()-(iLeft+pChild.getMeasuredWidth()), 0);
很抱歉,我的意思是pChild.layout调用的第三个参数为“iLeft+pChild.getMeasuredWidth()”,所以这不是问题所在。但是您的回答让我找到了问题的根源,这是父级的唯一布局,它显然通过相同的迭代和函数调用设置了它(子级)的布局!
pChild.layout(iLeft, 0, getMeasuredWidth()-(iLeft+pChild.getMeasuredWidth()), 0);