今天发现设置viewpager高度为wrap_content时并没作用,stackoverflow给出了解决方案,就是自定义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了

更多相关文章

  1. 关于Android的ActionBar不能显示左上角的图标解决方案
  2. Android(安卓)studio报错module not specified解决方案
  3. android实现再按一次退出程序
  4. Android实现点击某个按钮指定位置弹出布局
  5. Android(安卓)常用代码片小结
  6. Android(安卓)webView 软键盘覆盖输入框,webView页面底部显示不全
  7. android中控件公用产生的冲突的解决办法
  8. Android(安卓)获取View高度的4种方法
  9. Android(安卓)studio 编译错误:Execution failed for task ':app:

随机推荐

  1. 安卓自动化测试第二课
  2. Android通过串口与PC通信
  3. android 饼图
  4. 数组资源(arrays)的使用
  5. android EditText 默认情况下不获取焦点(
  6. Android中的Context
  7. Android 弹出对话框Dialog
  8. appt命令检测Apk信息的方法
  9. 安卓中的布局属性详解
  10. Android应用程序的安装位置