android ViewGroup的高度不能是wrap_content

时间:2012-11-30 05:03:40

标签: android viewgroup

我的视图组中会添加很多按钮,所以我需要我的viewgroup'height是wrap_content,但无论我在代码或xml中设置高度,它都不起作用,viewgroup'height总是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);
        }
    });

}

}

2 个答案:

答案 0 :(得分:0)

我做到了,但我仍然不知道为什么高度不能成为wrap_content; 我直接设置了viewgroup的实际高度,然后使用回调函数来改变高度;在CustomViewGroup中添加接口

public void setCallBack(CallBack c) {
    mCallBack = c;
}

public interface CallBack {
    public void callBack(int height);
}

然后在onlayout

if (lengthX > arg3) {
            Log.i("CustomViewGroup", "yes wrap");
            lengthX = width + VIEW_MARGIN + arg1;
            row++;
            lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height
                    + arg2;
            if (mCallBack != null) {
                mCallBack.callBack(lengthY);
            }
        }

在活动中 实现回调

private CallBack mCallBack = new CallBack() {

    @Override
    public void callBack(int height) {
        Log.i("CustomViewGroup", "callback");

        LayoutParams lp = (LayoutParams) vg.getLayoutParams();
        Log.i("CustomViewGroup", "vg height = " + lp.height + " height = " + height);
        lp.height = height;
        Log.i("CustomViewGroup", "vg height = " + lp.height + " height = " + height);
        vg.setLayoutParams(lp);
        vg.post(new Runnable() { //must be use post, if you dircetly vg.requestLayout, can't work, who can explain, appreciated!
            public void run() {
                vg.requestLayout();
            }
        });
        Log.i("CustomViewGroup", "vg height = " + vg.getHeight() + " height = " + height);

    }
};

答案 1 :(得分:0)

我通过从支持库复制ViewPager代码并稍微修改它来解决了这个问题。

我做的是:

  1. 将viewpager的代码复制到我的类CustomViewPager

  2. 扩展CustomViewPager并覆盖onMeasure方法

  3. 在这个方法中,我测量了第一个孩子的身高,并将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);
        }