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 这个组件,注意这个组件是用来显示左右滑动的界面的,如果不加载 ...
随机推荐
- (转)eclipse 启动参数介绍(如添加插件时,如果不显示,则使用eclipse -clean启动)
本文转载自:http://hi.baidu.com/dd_taiyangxue/blog/item/08950f3991b4e8c9d46225c8.html 其实,Eclipse是一个可以进行非常灵 ...
- nginx gzip on
# Gzip settings. gzip on; gzip_http_version 1.0;默认值是1.1 gzip_comp_level ; #压缩级别,1压缩比最小处理速度最快,9压缩比最大但 ...
- Application(basic)----Easyui
一,效果图. 二,源代码. <!DOCTYPE html><html><head> <meta charset="UTF-8"> & ...
- 基于jQuery带标题的图片3D切换焦点图
今天给大家分享一款基于jQuery带标题的图片3D切换焦点图.这款焦点图适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. 实现的代码. htm ...
- poj 3228 Gold Transportation 二分+网络流
题目链接 给出n个城市, 每个城市有一个仓库, 仓库有容量限制, 同时每个城市也有一些货物, 货物必须放到仓库中. 城市之间有路相连, 每条路有长度. 因为有些城市的货物量大于仓库的容量, 所以要运到 ...
- 树莓派高级GPIO库,wiringpi2 for python使用笔记(三)GPIO操作
GPIO库的核心功能,当然就是操作GPIO了,GPIO就是"通用输入/输出"接口,比如点亮一个LED.继电器等,或者通过iic spi 1-wire等协议,读取.写入数据,这都是G ...
- js判断某个方法是否存在
window.onload = function(){ try{ if(test && typeof(test) == "function"){ test(); } ...
- (iOS)Storyboard/xib小技巧
1.选择被view覆盖住的view 当你想直接在view中选择自己想要的元素时,但是又碍于一个view上叠加的元素太多很难直接选中,那么在这时,你同时按住键盘上的shift和 control键,然后在 ...
- asp.net core + angular2 的环境配置
国内整个对 asp.net core 和 angular2这些新出来的关注度不是太好.跟国外比很大差距. 我在试着去做这个整合的时候也碰到不少问题. 最后通过查阅大量资料才弄明白. 我想肯定也会有类 ...
- PCB板常用知识简介——沉金板VS镀金板
一.PCB板表面处理: 抗氧化,喷锡,无铅喷锡,沉金,沉锡,沉银,镀硬金,全板镀金,金手指,镍钯金 OSP: 成本较底,可焊性好,存储条件苛刻,时间短,环保工艺.焊接好 .平整 . 喷锡:喷锡板一般 ...