您的位置:首页 > 移动开发 > Android开发

android 开发中ScrollView 嵌套 ViewPager 不能显示解决办法

2016-11-16 15:46 471 查看
在ScrollView 中嵌套ViewPager 如果ViewPager高度使用wrap_content

就会出现无法显示问题

解决办法:

1.给ViewPager 设定固定高度(不推荐)

2.自定义ViewPager 重写ViewPager测量方法

<com.guanyueyun.mylook.view.CustomViewPager
android:id="@+id/view_home_main_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white" />


public class CustomViewPager extends ViewPager {

public CustomViewPager(Context context) {
super(context);
}

public CustomViewPager(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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android viewpager
相关文章推荐