重写ViewPager 里的onMeasure方法 然后在xml里引用这个WrapContentViewPager 就可以了
public class WrapContentViewPager extends ViewPager
{
/**
* @param context
*/
public WrapContentViewPager(Context context)
{
super(context);
// TODO Auto-generated constructor stub
}
/**
* @param context
* @param attrs
*/
public WrapContentViewPager(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see android.support.v4.view.ViewPager#onMeasure(int, int)
*/
@Override
protected void onMeasure(int arg0, int arg1)
{
// TODO Auto-generated method stub
int height=0;
for(int i=0;i<getChildCount();i++){
View v=getChildAt(i);
v.measure(arg0, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h=v.getMeasuredHeight();
if(h>height)
height=h;
}
arg1=MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(arg0, arg1);
}
}