viewpager的layout_width="wrap_content"无效问题
在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"无效问题的更多相关文章
- 【android】解决Viewpager设置高度为wrap_content无效的方法
今天发现设置viewpager高度为wrap_content时并没作用.stackoverflow给出了解决方式,就是自己定义viewpager,重写onMesure()方法: public clas ...
- RecyclerView android:layout_width="match_parent" 无效
使用RecyclerView 时,在xml文件中设置宽度match_parent无效. View view = mInflater.from(mContext).inflate(R.layout.it ...
- Android查缺补漏(View篇)--自定义 View 中 wrap_content 无效的解决方案
自定义 View 中 wrap_content 无效的解决方案 做过自定义 View 的童鞋都会发现,直接继承 View 的自定义控件需要重写 onMeasure() 方法,并设置 wrap_cont ...
- ViewPager不能高度自适应?height=wrap_content 无效解决办法
ViewPager用的很多,主要用啦展示广告条.可是高度却不能自适应内容,总是会占满全屏,即使设置android:height="wrap_content"也是没有用的.. 解决办 ...
- RelativeLayout设置wrap_content无效
尊重劳动成果,转载请标明出处:http://www.cnblogs.com/tangZH/p/8419053.html 在做项目的过程中,遇到了一个奇怪的现象,我设置RelativeLayout为的宽 ...
- Android ViewPager打造3D画廊
本文已授权微信公众号:鸿洋(hongyangAndroid)在微信公众号平台原创首发. 网上有很多关于使用Gallery来打造3D画廊的博客,但是在关于Gallery的官方说法中表明: This cl ...
- ViewPager及PagerTabStrip 的使用详解
ViewPager 就是一个滑屏效果的一个控件,使用比较简单.使用过程思路流程基本如下: 在需要添加的ViewPager的布局文件中添加ViewPager控件--->准备好滑屏所有的View-- ...
- Android ViewPager初探:让页面滑动起来
下一篇:<Android ViewPager再探:增加滑动指示条> ViewPager需要用到适配器PagerAAdapter,以下四个函数需要重写: instantiateItem(Vi ...
- ViewPager
1.ViewPager的功能为实现视图滑动 在主布局里边加入: <android.support.v4.view.ViewPager 这个组件,注意这个组件是用来显示左右滑动的界面的,如果不加载 ...
随机推荐
- CSS Sprite小图片自动合并工具
css-sprite是将css样式中零星的小图标,小图片合并成大图显示,这样能减小服务器并发连接数,减小服务器负载和带宽使用,有很高的实用价值.这里介绍一些自动合并图片并生成样式的工具. NodeJS ...
- HTML5新增加的功能
1.部分代码代替了以前的代码 例如: 获取焦点 旧:document.getElementById("price");.focus; 新:<input ...
- English - every和each的用法和区别
两者都有“每个”的意思,但用法不同: (1)each具有名词和形容词的功能,every只有形容词的功能. (2)each指两个或两个以上的人或事物中的“每个”:every是指三个以上的人或事物的“全体 ...
- log4j.xml配置示例
这是log4j1.x版本讲解,log4j-1.2.16.jar 1. 一般的log4j.xml的两种配置方式: 1.Logger 完成日志信息的处理定义输出的层次和决定信息是否输出DEBUG&l ...
- mlpack:可伸缩C++机器学习库(转)
摘要:mlpack是一个可伸缩C++机器学习库,它的目的是让新用户通过简单.一致的API使用机器学习,同时为专业用户提供C++的高性能和最大灵活性. mlpack是一个直观.快速.可伸缩的C++机器学 ...
- Win8安装ASP.net 4.5(转)
(原文:http://blog.csdn.net/dingxu_ren/article/details/17607451) 今天在我的电脑上部署Web程序时发现页面打不开,网上搜了下是因为先安装的.n ...
- jquery中的uploadfile关于图片上上传的插件的应用
ajaxFileUpload是一个异步上传文件的jQuery插件. 传一个不知道什么版本的上来,以后不用到处找了. 语法:$.ajaxFileUpload([options]) options参数说明 ...
- poj 1966 Cable TV Network 顶点连通度
题目链接 给一个图, n个点m条边, 求至少去掉多少个点可以使得图不再联通.随便指定一个点为源点, 枚举其他点为汇点的情况, 跑网络流, 求其中最小的情况. 如果最后ans为inf, 说明是一个完全图 ...
- 逻辑(if)、关系、数字、条件运算符
if语句 if-else if-else if(a==0){1} else if (a>0){2} else{3} 特点:1.2.3只有一个括号会被执行 陷阱 1.if(a!=0) ...
- POJ 3294 Life Forms(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=3294 [题目大意] 求出在至少在一半字符串中出现的最长子串. 如果有多个符合的答案,请按照字典序输出. [题解] 将所有的字符串通 ...