3

ビューグループに多くのボタンが追加されるので、ビューグループの高さがwrap_contentである必要がありますが、コードまたはxmlで高さを設定しても機能せず、ビューグループの高さは常にmatch_parentなので、何が問題なのですか? 高さをwrap_contentに設定するにはどうすればよいですか?

CustomViewGroup:

public class CustomViewGroup extends ViewGroup {

private final static String TAG = "CustomViewGroup";

private final static int VIEW_MARGIN = 2;

public CustomViewGroup(Context context) {
    super(context);
}

public CustomViewGroup(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Log.d(TAG, "widthMeasureSpec = " + widthMeasureSpec
            + " heightMeasureSpec" + heightMeasureSpec);

    for (int index = 0; index < getChildCount(); index++) {
        final View child = getChildAt(index);
        // measure
        child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
    Log.d(TAG, "changed = " + arg0 + " left = " + arg1 + " top = " + arg2
            + " right = " + arg3 + " botom = " + arg4);
    final int count = getChildCount();

    int row = 0;// which row lay you view relative to parent
    int lengthX = arg1; // right position of child relative to parent
    int lengthY = arg2; // bottom position of child relative to parent
    for (int i = 0; i < count; i++) {

        final View child = this.getChildAt(i);
        int width = child.getMeasuredWidth();
        int height = child.getMeasuredHeight();

        Log.i(TAG, "width = " + width + " height = " + height);
        lengthX += width + VIEW_MARGIN;
        lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height
                + arg2;
        Log.i(TAG, "lengthX = " + lengthX + " lengthY = " + lengthY + " row = " + row);
        // if it can't drawing on a same line , skip to next line
        if (lengthX > arg3) {
            lengthX = width + VIEW_MARGIN + arg1;
            row++;
            lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height
                    + arg2;

        }

        child.layout(lengthX - width, lengthY - height, lengthX, lengthY);
    }

}

}

public class ViewGroupTest extends Activity {

CustomViewGroup vg;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_group_test);
    LinearLayout l = (LinearLayout) findViewById(R.id.lllll);
    Button b1 = new Button(this);
    b1.setId(1);
    b1.setText("hello");
    vg = new CustomViewGroup(this);
    LinearLayout.LayoutParams pr = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,200);
    vg.setBackgroundColor(Color.BLUE);
    vg.setLayoutParams(pr);
    vg.addView(b1);
    Button b2 = new Button(this);
    b2.setId(2);
    b2.setText("world");
    vg.addView(b2);

    Button b3 = new Button(this);
    b3.setId(3);
    b3.setText("world 33333");
    vg.addView(b3);

    Button b4 = new Button(this);
    b4.setId(4);
    b4.setText("world44444444");
    vg.addView(b4);
    b3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Button b = new Button(ViewGroupTest.this);
            b.setText("world");
            vg.addView(b);
        }
    });

}

}

4

2 に答える 2

0

サポート ライブラリから ViewPager コードをコピーして少し変更することで、この問題を解決しました。

私がしたことはこれです:

  1. ビューページャーのコードをクラス CustomViewPager にコピーしました

  2. CustomViewPager の拡張と onMeasure メソッドのオーバーライド

このメソッド内で、最初の子の高さを測定し、ViewPager の高さを最初の子と同じに設定しました

 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mInLayout = true;
        populate();
        mInLayout = false;

        int firstChildHeight = 0;
        int firstChildWidth = 0;

        int size = getChildCount();

        for (int i = 0; i < size; ++i) {
            final View child = getChildAt(i);

            if (child.getVisibility() != GONE) {
                measureChild(child, widthMeasureSpec, heightMeasureSpec);

                if (i == 0) {
                    firstChildHeight = child.getMeasuredHeight();
                    firstChildWidth = child.getMeasuredWidth();
                }

                child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight());
            }
        }

        setMeasuredDimension(firstChildWidth, firstChildHeight);
    }
于 2013-01-31T20:33:08.237 に答える