在viewpager当中直接使用layout_width="wrap_content"是无效的,扩展了一下。解决这个问题。

package com.soulagou.ui;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
/**
*
* @author pangxg
* @title 扩展viewPager
* @description 和一般的ViewPager一样使用
*/
public class ViewPager extends android.support.v4.view.ViewPager{
//private static final String tag=ViewPager.class.getSimpleName();
protected ViewPager viewPager=null;
protected final int wrap_content=-;
protected final int match_parent=-;
protected int w =match_parent;
protected int h =match_parent;
protected int ph =match_parent;
protected int pw =match_parent; public ViewPager(Context context) {
super(context);
viewPager=this;
}
public ViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
viewPager=this;
}
@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
super.onLayout(arg0, arg1, arg2, arg3, arg4);
ViewGroup.LayoutParams params =null;
ph=viewPager.getLayoutParams().height;
pw=viewPager.getLayoutParams().width; Drawable bg= viewPager.getBackground();
if (bg!=null) {
int h = bg.getIntrinsicHeight();
int w= bg.getIntrinsicWidth();
if (ph==wrap_content) {
this.h=h;
}else if( ph==match_parent) {
this.h=ph;
} if (pw==wrap_content) {
this.w=w;
}else if( pw==match_parent) {
this.w=pw;
}
}
viewPager.setBackgroundColor(getResources().getColor(android.R.color.transparent));
ViewParent pv=viewPager.getParent();
if (pv instanceof LinearLayout) {
params = new LinearLayout.LayoutParams(this.w, this.h);
}else if (pv instanceof RelativeLayout) {
params = new RelativeLayout.LayoutParams(this.w, this.h);
}else if (pv instanceof FrameLayout) {
params = new FrameLayout.LayoutParams(this.w, this.h);
}
viewPager.setLayoutParams(params);
}
@Override
protected void onMeasure(int arg0, int arg1) {
super.onMeasure(arg0, arg1);
} }

在xml中和原来的viewPager一样使用就可以了。

<FrameLayout android:layout_marginTop="13dp"    android:layout_gravity="center_horizontal"   android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bord_indexbg">
<com.soulagou.ui.ViewPager
android:id="@+id/myindex_guidePager"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:background="@drawable/defa_advpic"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.soulagou.ui.ViewPager>
</FrameLayout>

viewpager的layout_width="wrap_content"无效问题的更多相关文章

  1. 【android】解决Viewpager设置高度为wrap_content无效的方法

    今天发现设置viewpager高度为wrap_content时并没作用.stackoverflow给出了解决方式,就是自己定义viewpager,重写onMesure()方法: public clas ...

  2. RecyclerView android:layout_width="match_parent" 无效

    使用RecyclerView 时,在xml文件中设置宽度match_parent无效. View view = mInflater.from(mContext).inflate(R.layout.it ...

  3. Android查缺补漏(View篇)--自定义 View 中 wrap_content 无效的解决方案

    自定义 View 中 wrap_content 无效的解决方案 做过自定义 View 的童鞋都会发现,直接继承 View 的自定义控件需要重写 onMeasure() 方法,并设置 wrap_cont ...

  4. ViewPager不能高度自适应?height=wrap_content 无效解决办法

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

  5. RelativeLayout设置wrap_content无效

    尊重劳动成果,转载请标明出处:http://www.cnblogs.com/tangZH/p/8419053.html 在做项目的过程中,遇到了一个奇怪的现象,我设置RelativeLayout为的宽 ...

  6. Android ViewPager打造3D画廊

    本文已授权微信公众号:鸿洋(hongyangAndroid)在微信公众号平台原创首发. 网上有很多关于使用Gallery来打造3D画廊的博客,但是在关于Gallery的官方说法中表明: This cl ...

  7. ViewPager及PagerTabStrip 的使用详解

    ViewPager 就是一个滑屏效果的一个控件,使用比较简单.使用过程思路流程基本如下: 在需要添加的ViewPager的布局文件中添加ViewPager控件--->准备好滑屏所有的View-- ...

  8. Android ViewPager初探:让页面滑动起来

    下一篇:<Android ViewPager再探:增加滑动指示条> ViewPager需要用到适配器PagerAAdapter,以下四个函数需要重写: instantiateItem(Vi ...

  9. ViewPager

    1.ViewPager的功能为实现视图滑动 在主布局里边加入: <android.support.v4.view.ViewPager 这个组件,注意这个组件是用来显示左右滑动的界面的,如果不加载 ...

随机推荐

  1. Oracle死锁。

    oracle数据库死锁一般情况下在oracle数据库中不会.但是在程序中可以开启事物没有提交,但是程序报错我们就关了程序在重新调试.但是我们程序总是在执行 comm.ExecuteNonQuery() ...

  2. java 设计模式初探之适配器模式

    1. 概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 2. 解决的问题 即Adapter模式使得原本由于接口不兼容而不 ...

  3. oracle习题SQL语句练习

    表(一)Student (学生表) 属性名 数据类型 可否为空 含 义 Sno Varchar2(3) 否 学号(主码) Sname Varchar2(8) 否 学生姓名 Ssex Varchar2( ...

  4. 详解new/delete(整合)

    C++中内存的动态分配与管理永远是一个让C++开发者头痛的问题,本文通过对C++中内存的动态分配释放的基本原理的介绍,让读者朋友能对C++中的内存的动态分配与释放有较为深入的理解,从而更好驾驭C++程 ...

  5. MFC的资源切换AFX_MANAGE_STATE(AfxGetStaticModuleState()

    转载自:http://blog.chinaunix.net/uid-20532101-id-1931929.html 以前写MFC的DLL的时候,总会在自动生成的代码框架里看到提示,需要在每一个输出的 ...

  6. BZOJ 1176: [Balkan2007]Mokia( CDQ分治 + 树状数组 )

    考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间 ...

  7. jQuery.validationEngine前端验证

    引入相关文件: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js" type ...

  8. System timers granularity

    http://code.google.com/p/javasimon/wiki/SystemTimersGranularity

  9. php执行shell更新svn文件的方法

    vim /etc/sudoers 修改内容如下: #Defaults !visiblepw Defaults visiblepw #Defaults requiretty <?php set_t ...

  10. smarty如何处理状态值的显示

    比如状态,有效或者无效.这个数据库中保存的是1或者2这样的字段. 显示在列表的时候不能是1或者2吧. 以前,我都是在后台foreach,处理的.感觉处理之后,前台就不灵活了.这个值就被替换成了文字. ...