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 这个组件,注意这个组件是用来显示左右滑动的界面的,如果不加载 ...
随机推荐
- Javascript基础篇小结
转载请声明出处 博客原文 随手翻阅以前的学习笔记,顺便整理一下放在这里,方便自己复习,也希望你有也有帮助吧 第一课时 入门基础 知识点: 操作系统就是个应用程序 只要是应用程序都要占用物理内存 浏览器 ...
- Jquer学习之jQuery(function(){})与(function(){})(jQuery)之间的区别
Jquery是优秀的Javascrīpt框架.我们现在来讨论下在 Jquery 中两个页面载入后执行的函数. $(document).ready(function(){ // 在这里写你的代码... ...
- 红豆带你从零学C#系列之:初识继承与多态
继承 现实生活当中,人类又可以根据职业分为:教师,学生,理发师,售货员 又比如飞机又有种类之分:直升飞机.客机.货机.战斗机等 在程序里面我们可能会通过创建类来描述这样的事物,比如学生类.教师类.理发 ...
- 使用VisualStudio发布ASP.NET网站
1.右击网站点击“发布网站” 2.选择或导入发布配置文件.→新建配置文件. 3.输入名称test.→点击确定. 4.发布方法选择文件系统. 5.选择目标位置.→点击下一步 6.文件发布选项选择三个选项 ...
- MyEclipse 免安装版制作
前言:以MyEclipse6.0为例,安装目录,例如如d:\java\MyEclipse6.0 (1)新建MyEclipse 6.0.bat文件 新建位置:当前MyEclipse根目录 文件内容: s ...
- linux杂记(四)热键[Tab],[ctrl]-c,[ctrl]-d,在线求助man page/info page
[Tab]按键 他具有[命令补全](接在一串指令的第一个字后面)与[档案补齐](接在第一串指令的第二字以后时)的功能.如 [KANO@kelvin ~]$ ca[tab][tab] cabextrac ...
- 四轴飞行器1.6 emwin与ucgui的移植,汉字外挂字库移植和DEMO效果对比
飞控的遥控器打算自己做,这样全局都能掌握,可以通过遥控器对飞控的参数和飞行模式进行修改,而买遥控器是做不到这样的哈..以后做图传的时候,屏幕还可以实时现实摄像头拍回来的画面,挺好的哈.. 做遥控我们选 ...
- maven打包成第三方jar包且把pom依赖包打入进来
<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId& ...
- Win7下使用protel99se、protel99
很多网友需要用的protel99se,由于该软件开发的时间比较就早,在现在笔记本普及,尤其是win7普及的情况下,用该软件着实让许多人头痛,有人选择安装XP系统,但这就牺牲了win7良好的操作体验.也 ...
- MyBatis使用DEMO及cache的使用心得
下面是一个简单的MyBatis使用DEMO. 整体结构 整体代码大致如下: POM依赖 需要引用两个jar包,一个是mybatis,另一个是mysql-connector-java,如果是maven工 ...