解决ViewPager高度无法wrap_content问题
来源:互联网 发布:数据统治世界 百度云 编辑:程序博客网 时间:2024/05/20 10:56
直接上解决方法,需要重写ViewPager中的onMeasure方法。
/** * ViewPager wrapContent解决方案 */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) {// super.onMeasure(widthMeasureSpec, heightMeasureSpec);//// // find the first child view// View view = getChildAt(0);// if (view != null) {// // measure the first child view with the specified measure spec// view.measure(widthMeasureSpec, heightMeasureSpec);// }//// setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view));// } @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); } /** * Determines the height of this view * * @param measureSpec A measureSpec packed into an int * @param view the base view with already measured height * * @return The height of the view, honoring constraints from measureSpec */ private int measureHeight(int measureSpec, View view) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { // set the height from the base view if available if (view != null) { result = view.getMeasuredHeight(); } if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; }}
0 0
- 解决ViewPager高度无法wrap_content问题
- 解决ViewPager 高度wrap_content无效问题
- 解决ViewPager 高度wrap_content无效问题
- 解决ViewPager 高度wrap_content无效问题 - 向大拿学习
- 解决ViewPager中RecyclerView的高度设为wrap_content无效的问题
- 【android】解决Viewpager设置高度为wrap_content无效的方法
- Android 解决ViewPager设置高度为wrap_content无效的方法 MyViewPager
- ViewPager高度自适应?height=wrap_content 无效、固定高度适配性差
- 解决viewpager嵌套viewpager,子viewpager无法滑动的问题
- 解决 viewpager 不能使用wrap_content 属性
- ViewPager wrap_content
- 自定义ViewPager实现高度自适应 WRAP_CONTENT|包裹内容
- ViewPager不能高度自适应?height=wrap_content 无效解决办法
- ViewPager不能高度自适应?height=wrap_content 无效解决办法
- ViewPager不能高度自适应?height=wrap_content 无效解决办法
- 解决ViewPager中Gallery无法滑动问题
- 解决ViewPager中Gallery无法滑动问题
- ViewPager自适应高度问题
- Win32串口编程
- 继承NSOperation-自定义异步 Operation
- HDU 5783 Divide the Sequence(贪心)
- 读《About Face 4 交互设计精髓》13
- 整数型int 为什么不可以用 memset 函数?
- 解决ViewPager高度无法wrap_content问题
- 我的jquery之路(二)
- 文章标题
- sed工具以及awk工具的介绍及使用
- iOS开发 非常全的三方库、插件、大牛博客等等
- netbeans c++环境配置
- 基础图论知识总结
- jdbc查询大量数据内存溢出的解决方法
- 初次写博客,留作纪念