0

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

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

这是图像:

Candy Crush 图标(部分移动)应该移动到 Coin Pirates 图标(位于底部,大部分被 Candy Crush 图标隐藏)的右侧

我设法将 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();
    }
}

任何帮助、示例或教程和解释都非常感谢......我已经在反复试验中浪费了数周时间,因为我找不到任何关于它的资源。 ,这非常令人沮丧:

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),因此会发生剪切

4

1 回答 1

1

我想问题出在这里:

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

第三个和第四个参数是相对于父级的右侧和底部距离。所以尝试这样的事情:

pChild.layout(iLeft, 0, getMeasuredWidth()-(iLeft+pChild.getMeasuredWidth()), 0);
于 2013-07-29T10:17:50.833 回答