2013-07-29 50 views
0

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

我已经设置了一点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)

我设法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下记录和说,这是非常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迭代每个孩子并调用他们的布局(iTop,iLeft,iRight,iBottom);功能很好,因为它是设置孩子们的布局(0,0,iWidth,IHEIGHT),发生削波

回答

1

我想这个问题是在这里:

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

第三和第四ARG游戏的权利和相对于父母的最低距离。因此,尝试这样的事情:

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

我很抱歉,我的意思是“iLeft + pChild.getMeasuredWidth()”;关于pChild.layout电话是第三个参数,所以这不是问题.. 但你的答案将我引向问题的源头,这是父级的onLayout,它明显通过相同的迭代和函数调用来设置它的(孩子)布局! – Zennichimaro

相关问题