【android】解决Viewpager设置高度为wrap_content无效的方法
16lz
2021-01-24
今天发现设置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了
更多相关文章
- 关于Android的ActionBar不能显示左上角的图标解决方案
- Android(安卓)studio报错module not specified解决方案
- android实现再按一次退出程序
- Android实现点击某个按钮指定位置弹出布局
- Android(安卓)常用代码片小结
- Android(安卓)webView 软键盘覆盖输入框,webView页面底部显示不全
- android中控件公用产生的冲突的解决办法
- Android(安卓)获取View高度的4种方法
- Android(安卓)studio 编译错误:Execution failed for task ':app: