0

我正在尝试编写自己的布局类。但我在使用 onLayout 时遇到了问题。如果我扩展 LinearLayout 并让它执行 onLayout 并在我的 onMeasure 中调用 super.onMeasure(),一切正常。但它无法扩展 ViewGroup。显示的是最左边的第一个孩子被正确渲染,但其余的没有出现或尺寸不正确。

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

class ManualPanel extends ViewGroup
{
    private int totalDivs;
    private int[] sizesArr;

    public ManualPanel(final Context context)
    {
        this(context, null);
    }

    public ManualPanel(final Context context, final AttributeSet attrs)
    {
        super(context, attrs);

        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ManualPanel);

        final String[] sizeStr = a.getString(R.styleable.ManualPanel_sizes).split("/");
        final String[] fracArr = sizeStr[0].split(",");
        a.recycle();

        int sum = 0;
        sizesArr = new int[fracArr.length];
        for (int i = 0; i < sizesArr.length; i++) {
            sizesArr[i] = Integer.valueOf(fracArr[i]);
            sum += sizesArr[i];
        }
        totalDivs = Math.max(sum, Integer.valueOf(sizeStr[1]));
    }

    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec)
    {
        final int children = getChildCount();
        final int totalWidth = MeasureSpec.getSize(widthMeasureSpec);
        final int totalHeight = MeasureSpec.getSize(heightMeasureSpec);

        totalDivs = Math.max(totalDivs, children);

        final int[] arr = IntArray.copyOf(sizesArr, children);
        final int width[] = new int[children];
        int sum = 0;

        for (int i = 0; i < children; i++) {
            width[i] = (arr[i] * totalWidth) / totalDivs;
            sum += width[i];
        }
        for (int i = 0, diff = totalWidth - sum; diff > 0; i = ++i % children) {
            width[i]++;
            diff--;
        }
        for (int i = 0; i < children; i++) {
            final View view = getChildAt(i);
            final LayoutParams lp = view.getLayoutParams();

            lp.width = width[i];
            view.setLayoutParams(lp);
            view.measure(MeasureSpec.EXACTLY | lp.width, MeasureSpec.EXACTLY | totalHeight);
        //  view.measure(MeasureSpec.AT_MOST | lp.width, MeasureSpec.AT_MOST | totalHeight);
        }
        setMeasuredDimension(totalWidth, totalHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        final int children = getChildCount(), height = getMeasuredHeight();
        for (int i = 0; i < children; i++) {
            final View view = getChildAt(i);

            if (view.getVisibility() != View.VISIBLE)
                continue;

            final int width = view.getMeasuredWidth();
            view.layout(l, 0, left + width, height);
            l += width;
        }
    }
}
4

2 回答 2

1

ViewGroup 不知道如何放置孩子。或者如何绘制它们。你必须自己做。

于 2012-03-01T21:09:45.043 回答
0

我还没有尝试过你的布局。但这是我所看到的。看你的线

view.layout(l, 0, left + width, height);

您的left变量似乎总是等于零,因为您没有在循环内更改它。

于 2012-03-01T21:30:34.090 回答