解决ViewPager 高度wrap_content无效问题

本文提供了一种解决 ViewPager 在设置为 wrap_content 时无法正常显示子内容的问题的方法。通过继承 ViewPager 并重写 onMeasure 方法,确保了 ViewPager 能够正确测量其高度,从而在布局中适当地展示其内容。

今天做了的项目中.用了tabhost,然后下边切换部分用的viewPager.当我把viewPager设置为wrap_content的时候,发现.怎么也不能显示其中的子内容.然后百度一顿搜.没找到真正能完成的结果.然后google.在stackflow里发现了老外解决了该问题.下边把代码下!

/**
 * ViewPager wrapContent解决方案
 */
public class WrapContentHeightViewPager extends ViewPager {/**
     * Constructor
     *
     * @param context the context
     */
    public WrapContentHeightViewPager(Context context) {
        super(context);
    }

    /**
     * Constructor
     *
     * @param context the context
     * @param attrs the attribute set
     */
    public WrapContentHeightViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

//    @Override//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);//
//        // find the first child view//        View view = getChildAt(0);//        if (view != null) {//            // measure the first child view with the specified measure spec//            view.measure(widthMeasureSpec, heightMeasureSpec);//        }//
//        setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view));//    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int height = 0;
        for(int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if(h > height) height = h;
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    /**
     * Determines the height of this view
     *
     * @param measureSpec A measureSpec packed into an int
     * @param view the base view with already measured height
     *
     * @return The height of the view, honoring constraints from measureSpec
     */
    private int measureHeight(int measureSpec, View view) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            // set the height from the base view if available
            if (view != null) {
                result = view.getMeasuredHeight();
            }
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

}
转载自: http://www.tuicool.com/articles/2YrmE3
<com.android.camera.ui.RotateLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:camera="http://schemas.android.com/apk/res/com.mediatek.camera0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:splitMotionEvents="false" > <com.android.camera.ui.MaxLinearLayout android:id="@+id/container" style="@style/htfSettingContainer" android:splitMotionEvents="false"> <!-- add by yanglh start --> <RelativeLayout android:id="@+id/setting_header" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF" android:layout_marginTop="30dp" > <ImageButton android:id="@+id/btn_back" android:layout_width="48dp" android:layout_height="48dp" android:layout_centerVertical="true" android:layout_alignParentStart="true" android:src="@drawable/htf_back_icon" android:background="@drawable/htf_round_back" android:contentDescription="back" /> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginStart="50dp" android:text="@string/pref_camera_settings_category" android:textSize="18sp" android:textColor="#000000"/> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#1AC0C0C0" /> <!-- add by yanglh end --> <TabHost android:id="@+id/tab_title" style="@style/SettingTabTitle"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg_pressed" android:splitMotionEvents="false"> </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" android:splitMotionEvents="false"> </FrameLayout> </TabHost> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"/> </com.android.camera.ui.MaxLinearLayout> </com.android.camera.ui.RotateLayout>在当前布局中添加属性使其可以上下滑动
最新发布
11-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值