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

时间:2013-07-29 10:05:21

标签: android layout

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

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

这是图片:

The Candy Crush Icon (which is partially moved) was suppose to be moved to the right of the Coin Pirates icon (which was on the bottom and mostly hidden by the Candy Crush icon)

我设法将Candy Crush图标向右移动了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();
    }
}

任何帮助,示例或教程和解释都非常受欢迎......我一直在浪费几周的时间进行试验和错误,因为我找不到任何关于它的资源.onMeasure和onLayout正在记录中,而不是按照说法工作,这非常令人沮丧:

public void layout (int l, int t, int r, int b)

Assign a size and position to a view and all of its descendants

This is the second phase of the layout mechanism. (The first is measuring). In this phase, each parent calls layout on all of its children to position them. This is typically done using the child measurements that were stored in the measure pass().

Derived classes should not override this method. Derived classes with children should override onLayout. In that method, they should call layout on each of their children.

Parameters
l   Left position, relative to parent
t   Top position, relative to parent
r   Right position, relative to parent
b   Bottom position, relative to parent 

编辑:

澄清答案

正如@ Ben75指出的那样,问题是由于错误地设置了pChild.layout(iTop,iLeft,iRight,iBottom)引起的。值。但是,pChild.layout不是视图的onLayout调用的那个,而是父节点onLayout调用的那个。父母的onLayout遍历每个孩子并调用他们的布局(iTop,iLeft,iRight,iBottom);函数也是如此,因为它将子项的布局设置为(0,0,iWidth,iHeight),所以会发生剪辑

1 个答案:

答案 0 :(得分:1)

我想问题出在这里:

pChild.layout(iLeft, 0, pChild.getMeasuredWidth(), pChild.getMeasuredHeight());

第三和第四个args是相对于父级的右下角距离。所以尝试这样的事情:

pChild.layout(iLeft, 0, getMeasuredWidth()-(iLeft+pChild.getMeasuredWidth()), 0);