Android的 - 如何设置位置的儿童onLayout查看 [英] Android - how to set position of children view in onLayout

查看:331
本文介绍了Android的 - 如何设置位置的儿童onLayout查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何设置的Andr​​oid子视图的位置?

How do we set the position of subview in Android?

我已经成功地将其设置成onLayout()一点点,但我只能这样做是为了一定的价值,在这之后,子视图将不会显示。我认为这是越来越裁剪,但我想不出为什么它得到裁剪以及如何正确地做到这一点。

I've managed to set it a little bit in onLayout() but I can only do it to a certain value, after which, the subview will not show up. I think it is getting cropped but I cannot figure out why it get cropped and how to do it properly.

下面是图像:

我设法10移动糖果粉碎右边的图标,如果我这样做多了,这一切都不会显示出来...:(

I managed to move the Candy Crush icon by 10 to the right, if I do it more, all of it will not show up... :(

下面是code:

    @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();
    }
}

任何帮助,或样品,或教程,并解释是高度AP preciated ......我已经浪费了周的试错,因为我找不到它的任何资源。该onMeasure和onLayout正在记录并没有担任所说,这是非常frustating:

Any help, or sample, or tutorial and explanation is highly appreciated... I've been wasting weeks in trial and errors, as I cannot find any resources about it.. The onMeasure and onLayout is under documented and is not working as said, which is very frustating:

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遍历每个孩子并调用它们的布局(艾拓,iLeft,iRight,iBottom);作为好,因为它是孩子们的布局设置为(0,0,iWidth,IHEIGHT),发生削波

As pointed out by @Ben75, the problem is caused by incorrectly setting the pChild.layout(iTop, iLeft, iRight, iBottom); values. However, the pChild.layout is not the one called by the view's onLayout but is the one called by the parent's onLayout. The parent's onLayout iterates through every children and call their layout(iTop,iLeft,iRight,iBottom); function as well, and since it is setting the children's layout to (0,0,iWidth,iHeight), the clipping occurs

推荐答案

我想这个问题是在这里:

I guess the problem is here:

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

第三和第四次ARGS是相对于父右侧和底部的距离。因此,尝试这样的:

Third and fourth args are right and bottom distances relative to parent. So try something like this:

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

这篇关于Android的 - 如何设置位置的儿童onLayout查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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