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 这个组件,注意这个组件是用来显示左右滑动的界面的,如果不加载 ...
随机推荐
- 权威指南学习心得-浏览器中的js
window对象:表示web了浏览器的一个窗口或窗体(winow属性引用自身) 含有以下属性:location包含Location对象,指定当前显示在窗口中URL,允许脚本往窗口里载入新的URL 含有 ...
- 【百度地图API】获取行政区域的边界
);map.addControl(new BMap.NavigationControl({type: BMAP_NAVIGATION_CONTROL_SMALL}));map.enableScroll ...
- 一键搞定Java桌面应用安装部署 —— exe4j + Inno Setup 带着JRE, 8M起飞
转载自:http://www.blogjava.net/huliqing/archive/2008/04/18/193907.html 对于作Java桌面应用来说,比较烦人的就是安装部署问题,客户端是 ...
- JavaSE思维导图(六)
- BZOJ 4000: [TJOI2015]棋盘( 状压dp + 矩阵快速幂 )
状压dp, 然后转移都是一样的, 矩阵乘法+快速幂就行啦. O(logN*2^(3m)) ------------------------------------------------------- ...
- ROS验证publisher和subscriber
在前面的两篇博客中我们用C++在ROS中创建了一个发布者和接收者,并使用catkin_make构建了新的节点,下面就需要验证一下,我们写的是否正确. 首先运行roscore roscore 在使用ca ...
- python 推导式和迭代器、生成器
1.常用推导式 推导式是从一个或者多个迭代器快速简洁创建数据结构的一种方法. 1.1 _ 列表推导式 最简单的形式: [exprssion for item in iterable] 示例: nu ...
- Word2Vec在Tensorflow上的版本以及与Gensim之间的运行对比
接昨天的博客,这篇随笔将会对本人运行Word2Vec算法时在Gensim以及Tensorflow的不同版本下的运行结果对比.在运行中,参数的调节以及迭代的决定本人并没有很好的经验,所以希望在展出运行的 ...
- Groovy中那些神奇注解之ToString
继续上一篇:Groovy中那些神奇注解之Memoized 这篇就讲讲@groovy.transform.ToString这个注解,这注解太熟悉了,熟悉到让人一看就知道是干吗的,不就是把Bean转在St ...
- Java基础—ClassLoader的理解(转)
默认的三个类加载器 Java默认是有三个ClassLoader,按层次关系从上到下依次是: Bootstrap ClassLoader Ext ClassLoader System ClassLoad ...