android ViewGroup代码里addView时,子View的布局设置wrap_content失效问题

具体子View的测量模式和大小请看下表:

android ViewGroup代码里addView时,子View的布局设置wrap_content失效问题_第1张图片

解决方法:

View childView = View.inflate(this, R.layout.guideview_left, null);

//测量view的宽高
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
childView.measure(w, h);
//获取子View的宽高
int mWidth = childView.measuredWidth;
int mHeight = childView.measuredHeight;

RelativeLayout.LayoutParams param = RelativeLayout.LayoutParams(mWidth, mHeight );
//把子View添加到ViewGroup里面
mViewGroup.addView(childView , param);

你可能感兴趣的:(Android)