• 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=-2;
        protected final int match_parent=-1;
        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>
  • 相关阅读:
    DateUtil-工具类
    文件上传到服务器
    删除文件以及文件夹
    Git命令<转载 https://www.cnblogs.com/cspku/articles/Git_cmds.html>
    httpd似乎没有在运行,并且正在代理补鞋匠,或者SELinux正在这样做。
    【转载】使用yum安装服务报错:[Errno 14] curl#6
    Cobbler自动化安装部署操作系统
    恢复或重置root密码(Centos7)
    DNS服务器
    Windows Server 2008安装教程
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4818665.html
Copyright © 2020-2023  润新知