【android】解决Viewpager设置高度为wrap_content无效的方法
来源:互联网 发布:浪潮数据库服务器 编辑:程序博客网 时间:2024/05/23 15:10
今天发现设置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 1
- 【android】解决Viewpager设置高度为wrap_content无效的方法
- Android 解决ViewPager设置高度为wrap_content无效的方法 MyViewPager
- 解决ViewPager中RecyclerView的高度设为wrap_content无效的问题
- 解决ViewPager 高度wrap_content无效问题
- 解决ViewPager 高度wrap_content无效问题
- ViewPager设置wrap_content无效
- 解决ViewPager 高度wrap_content无效问题 - 向大拿学习
- ViewPager高度自适应?height=wrap_content 无效、固定高度适配性差
- 解决ViewPager高度无法wrap_content问题
- 使用ViewPager,设置为wrap_content后没有效果,以及多个view高度不同时ViewPager高度自适应
- 一句话解决RecyclerView 高度设置wrap_content 无作用的bug
- ViewPager不能高度自适应?height=wrap_content 无效解决办法
- ViewPager不能高度自适应?height=wrap_content 无效解决办法
- ViewPager不能高度自适应?height=wrap_content 无效解决办法
- android 解决vedeoView设置高度无效的问题
- android-自定义View解决wrap_content无效的问题
- android-自定义View解决wrap_content无效的问题
- android-自定义View解决wrap_content无效的问题
- UIActivityIndicatorView
- 工厂方法——Factory Method
- 三个小招式,教你优雅地对付霸占你电脑的熊孩子
- PHP安装包TS和NTS的区别
- hibernate 使用mysql6.0 无法自动建表的解决办法
- 【android】解决Viewpager设置高度为wrap_content无效的方法
- 想成为优秀产品经理?别忽视这十本好书
- 【Qt】仿QQ表情选择控件
- HDU-#1115 Lifting the Stone(多边形重心)
- 什么是IT
- 三层架构和MVC模型的常识
- 线程安全,有状态,无状态的对象
- WordPress站点设置伪静态化
- js 多种变量定义(对象直接量,数组直接量和函数直接量)