【问题标题】:android ViewGroup height can't be wrap_contentandroid ViewGroup 高度不能是 wrap_content
【发布时间】:2012-11-30 05:03:39
【问题描述】:

在我的视图组中会添加很多按钮,所以我需要我的视图组'高度是 wrap_content,但是无论我在代码或 xml 中设置高度,它都不起作用,视图组'高度总是 match_parent,所以有什么问题它?如何设置高度为 wrap_content?

自定义视图组:

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);
        }
    });

}

}

【问题讨论】:

    标签: android viewgroup


    【解决方案1】:

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

    我做的是这样的:

    1. 将 viewpager 的代码复制到我的类 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);
        }
    

    【讨论】:

      【解决方案2】:

      我做到了,但我还是不知道为什么高度不能是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);
      
          }
      };
      

      【讨论】:

        猜你喜欢
        • 2011-08-23
        • 2017-11-27
        • 1970-01-01
        • 2019-01-23
        • 1970-01-01
        • 2017-04-22
        • 2013-12-21
        • 2020-07-07
        • 2012-05-20
        相关资源
        最近更新 更多