ViewPager不能高度自适应?height=wrap_content 无效解决办法
2016-04-13 09:44
417 查看
ViewPager用的很多,主要用啦展示广告条。可是高度却不能自适应内容,总是会占满全屏,即使设置android:height="wrap_content"也是没有用的。。解决办法其实网上有很多,但是个人感觉不是很好
这段代码中,就用了weight来保证ViewPager始终占满屏幕的剩余空间。如果ViewPager里面的内容不需要那么高,怎么办?这个方法就不行了。
?
比如:LinearLayout的时候,使用weight来自动调整ViewPager的高度。
一般的代码如下:
?123456789101112131415161718192021222324 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:orientation = "vertical" > < android.support.v4.view.ViewPager android:id = "@+id/pager" android:layout_width = "fill_parent" android:layout_height = "0dp" android:layout_weight = "1.0" /> < ImageView android:id = "@+id/ivCursor" android:layout_width = "60dp" android:layout_height = "5dp" android:scaleType = "fitCenter" android:src = "@drawable/cursor" /> < LinearLayout android:id = "@+id/tabs" android:layout_width = "fill_parent" android:layout_height = "wrap_content" /> </ LinearLayout > |
还比如:固定ViewPager的高度。height="100dp"。
这样也不是很好。当服务器为了保证图片在不同dpi的手机上,不被缩放,返回的图片高度也有可能不同,固定高度就造成了不能很好的适应这钟变化。在实际开发中,本人用的最多的就是通过LayoutParmas动态改变ViewPager的高度。
个人感觉这个方法不错还比较简单。在给ViewPager设置View的时候,通过获取view的高度,动态的设置ViewPager的高度等于view的高度,就OK了。?
12345 | int viewPagerIndex = main.indexOf(viewPager); int childViewHeight = getChildViewHeight(); //获取ViewPager的子View的高度。 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, childViewHeight ); //这里设置params的高度。 main.removeView(viewPager); main.addView(viewPager, viewPagerIndex , params); //使用这个params |
或者,直接继承ViewPager,在onMeasure中返回childView的高度。
这样布局的时候,就会使用childView的高度了。思路和上面一样。代码如下:?1234567891011121314151617181920212223242526272829303132333435 | import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.View; public class WrapContentHeightViewPager extends ViewPager { public WrapContentHeightViewPager(Context context) { super (context); } public WrapContentHeightViewPager(Context context, AttributeSet attrs) { super (context, attrs); } @Override protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) { int height = 0 ; //下面遍历所有child的高度 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) //采用最大的view的高度。 height =h; } heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); super .onMeasure(widthMeasureSpec,heightMeasureSpec); } } |
相关文章推荐
- Android开发网上的一些重要知识点(2)
- Android开发网上的一些重要知识点(1)
- Android获取屏幕高度、状态栏高度、标题栏高度
- android 获取屏幕高度和宽度 的方法
- EditText的一点儿补充
- 关于陌陌和微信表情页与输入法之间切换的问题
- DJANGO中migration(迁移)的理解和运用
- 剑指offer 37 数字在排序数组中出现的次数
- CMD模块定义规范
- 从亚马逊、微软、谷歌的云之争,看国内云计算的未来
- mysql事务处理用法与实例详解
- 完美解决点击空白处,隐藏软键盘
- Android, 获取虚拟键盘的高度
- android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed错误原因
- sqlite timestamp 时间字段查询语句的写法
- android 阶段性日期的获取,向前或者向后推一段时间
- 如何监听显示安卓软键盘事件
- java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams
- ConcurrentModificationException异常解决办法
- android中GridView关于间距的属性值介绍