就是自定义viewpager,重写onMesure()方法:
public class WrapContentHeightViewPager extends ViewPager {
/**
* Constructor
*
* @param context the context
*/
public WrapContentHeightViewPager(Context context) {
super(context);
}
/**
* Constructor
*
* @param context the context
* @param attrs the attribute set
*/
public WrapContentHeightViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = 0;
for(int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if(h > height) height = h;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
使用的时候用WrapContentHeightViewPager代替viewpager就ok了
android ViewPager使用了height为wrap_content无效,希望自适应内容高度
在ScrollView 里面,就算你设置为fill_parent也是没用的
要么只能写死,比如写成 180dp 这样
android在代码中设置控件(ScrollView)的高度
一般View像你说的那样便得了,如果那样不行的话可能Scrollview比较特殊一点,我一般都是让ScrollView wrap_content ,然后ScrollView里的布局不要wrap_content,比如里面嵌套一个RelaytiveLayout,这个RelaytiveLayout的高度写死。你代码中设置这个RelaytiveLayout的高度试试能不能满足你的需求