LayoutManager中的子位置

时间:2015-07-29 15:07:05

标签: android android-layout

我有一个LayoutManager,我想将子位置更改为右侧。我已经改变了child.layout方法,但它还没有解决问题。

如何更改child.layout(),以获得解决方案? 我找到了THIS LINK但没有为我工作

以下是onLayout的代码:

@Override
    public void onLayout(boolean changed, int l, int t, int r, int b) {
        //Element position
        //Own position

        int mRowHeight = 0;
        int left, top, right;
        int elementsSizeInRow;
        int actItem = 0;
        int remainAreainRow = 0;


        for (int i = 0; i < rowCount.length; i++) {
            int mRowWidth = 0;

            elementsSizeInRow = 0;

            for (int j = 0; j < rowCount[i]; j++) {
                View child = getChildAt(actItem);

                elementsSizeInRow += child.getMeasuredWidth();
                //Log.v("ItemSizes",":"+child.getMeasuredWidth());
                //Log.v("RowItemSize","RowItemsSize: "+elementsSize);

                left = mRowWidth;
                top = mRowHeight;

                mRowWidth += child.getMeasuredWidth();
                child.layout(left, top, left + child.getMeasuredWidth(), top + child.getMeasuredHeight());
//changed to forexample child.layout(left, top, left + getMeasuredWidth()-(left+child.getMeasuredWidth()), top + child.getMeasuredHeight());   //but it's not a solution               

                actItem++;
                remainAreainRow = getMeasuredWidth() - elementsSizeInRow;

            }

            mRowHeight += rowHeight[i];

1 个答案:

答案 0 :(得分:0)

哦,我解决了问题:) 谢谢你什么都没有! ;)

解决方案:

我计算了所有行的剩余大小,我添加了onLayout第一个和第三个参数像这样:

child.layout(remainAreainRow, top, remainAreainRow + child.getMeasuredWidth(), top + child.getMeasuredHeight());

它正在工作,孩子们在右侧,顶部和底部的重力可以设置在&#34; Linearlayout android:gravity =&#34; center,top,bottom&#34 ;&#34;关于XML。