ViewPager用的很多,主要用啦展示广告条。可是高度却不能自适应内容,总是会占满全屏,即使设置android:height="wrap_content"也是没有用的。。

解决办法其实网上有很多,但是个人感觉不是很好

比如:LinearLayout的时候, 使用weight来自动调整ViewPager的高度。

一般的代码如下:

<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>

这段代码中,就用了weight来保证ViewPager始终占满屏幕的剩余空间。如果ViewPager里面的内容不需要那么高,怎么办?这个方法就不行了。

还比如:固定ViewPager的高度。height="100dp"。

这样也不是很好。当服务器为了保证图片在不同dpi的手机上,不被缩放,返回的图片高度也有可能不同,固定高度就造成了不能很好的适应这钟变化。

在实际开发中,本人用的最多的就是通过LayoutParmas动态改变ViewPager的高度。

个人感觉这个方法不错还比较简单。

在给ViewPager设置View的时候,通过获取view的高度,动态的设置ViewPager的高度等于view的高度,就OK了。

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的高度了。思路和上面一样。代码如下:

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 = ;
//下面遍历所有child的高度
for (int i = ; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec,
MeasureSpec.makeMeasureSpec(, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if (h > height) //采用最大的view的高度。
height = h;
} heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

ViewPager不能高度自适应?height=wrap_content 无效解决办法的更多相关文章

  1. JQuery iframe宽高度自适应浏览器窗口大小的解决方法

    iframe宽高度自适应浏览器窗口大小的解决方法   by:授客 QQ:1033553122 1.   测试环境 JQuery-3.2.1.min.js 下载地址: https://gitee.com ...

  2. iOS 发布证书提示 此证书的签发者无效 解决办法

    1. 打开钥匙串  查看发布证书 都是提示 此证书的签发者无效   解决办法 : 2. 到了 第 4 步骤 再去 查看 发布证书 就会 显示  此证书有效 3.  如果还不可以 就 把 Apple W ...

  3. IDEA Spring-boot-devTools 无效解决办法二

    转载地址:Intellij IDEA 使用Spring-boot-devTools无效解决办法 相信大部分使用Intellij的同学都会遇到这个问题,即使项目使用了spring-boot-devtoo ...

  4. HTTP 错误 500.19 请求的页面的相关配置数据无效 解决办法

    "HTTP 错误 500.19 请求的页面的相关配置数据无效" 解决办法   HTTP 错误 500.19 - Internal Server Error无法访问请求的页面,因为该 ...

  5. 在vue中使用 layui框架中的form.render()无效解决办法

    下面简单介绍在vue中使用 layui框架中的form.render()无效解决办法. 原文地址:小时刻个人技术博客 > http://small.aiweimeng.top/index.php ...

  6. CSS高度自适应 height:100%;

    在初次尝试高度自适应时都会遇到这样的问题: 对象的heith:100%; 并不能直接产生实际效果 为什么呢?之所以没有效果,与浏览器的解析方式有一定关系,查看下面代码 <!DOCTYPE htm ...

  7. chrome浏览器font-size<12px无效解决办法

    当样式设定font-size<12px时,chrome浏览器里字体显示仍为12px:如font-size:11px; 但是chrome还是12px的大小,很不听话. 今天我就遇到了这样的问题?网 ...

  8. 设置placeholder无效解决办法

    一.设置placeholder的方法 placeholder属性用来设置控件内部的提示信息 <input type="text" placeholder="请输入用 ...

  9. windows7下修改hosts文件无效解决办法(转)

    通常会为了开发方便.或者屏蔽掉一些恶意网站,我们会在hosts(c:\windows\system32\drivers\etc\hosts)文件中进行相应的域名指向,例:

随机推荐

  1. leetcode Combination Sum II python

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. QF——对不同尺寸屏幕的适配(自动布局:AutoLayout)

    对不同尺寸设备UI的适配: 很多时候,我们的App可能运行在不同尺寸的设备上,或者横竖屏时,呈现方法应该也不一样.这样便要求UI里各控件的位置和大小不能写死. 对于不同尺寸UI的适配,一般有三种对策: ...

  3. select * from (select P.*,ROWNUM RN FROM(select * from Mp_Relatedart where pubbaseid=785 order by ID ASC )P)M WHERE M.RN>2 and M.RN <= 7

    select * from (select P.*,ROWNUM RN FROM(select * from Mp_Relatedart where pubbaseid=785 order by ID ...

  4. jenkins 配置安全邮件

    Jenkins网页设置界面只支持SSL协议 ,对于STARTTLS协议,需要修改jenkins的配置文件去支持基于TLS的SMTP认证 1.修改jenkins配置文件 打开jenkins配置文件/et ...

  5. 【Howie玩docker】-docker安装

    windows忽略,小苹果木有,所以咱只看ubuntu和centOS的吧! 参考书<Docker技术入门与实战> Ubuntu 14.04安装Docker Ubuntu 14.04版本官方 ...

  6. CeontOS7安装ansible

    安装方法一. 第一步:安装epel rpm -ivh http://mirror.pnl.gov/epel/7/x86_64/e/epel-release-7-5.noarch.rpm 第二步:安装a ...

  7. SQL Server 全文索引

    create table Document(ID int not null,Name nvarchar(255) not null,Body nvarchar(max) not null);go cr ...

  8. Combotree,datebox 启用 禁用

    combotree <input type="checkbox" id="ckMonitor"></input> <input i ...

  9. Oracle EBS-SQL (SYS-6):sys_在线用户职责查询.sql

    /*线用户查询-1*/ SELECT FSAV.USER_NAME,FU.DESCRIPTION,FSAV.RESPONSIBILITY_NAME,FSAV.USER_FORM_NAME,FSAV.L ...

  10. Qt for Windows:使用WinPcap开发高性能UDP服务器

    首先介绍一下WinPcap WinPcap是Windows下一个网络库,性能极其强悍而且能够接收各种包. 大名鼎鼎的WireShark就是基于这个库开发的. 那么这个库性能到底有多高呢. 我测试了UD ...