如果设置layout_height = wrap_content,则ViewPager无法正常工作 [英] ViewPager not working if set layout_height = wrap_content

查看:63
本文介绍了如果设置layout_height = wrap_content,则ViewPager无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ViewPaper 时遇到问题.设置ViewPaper的 height = wrap_content 时,它什么都没有显示,并且显示了是否为 ViewPager 设置了高度(例如: android:layout_height ="350dp" ).请帮忙!谢谢!

I got issues when worked with ViewPaper. It doesn't show anything when I set ViewPaper's height = wrap_content and it shows if I set height for ViewPager (ex: android:layout_height= "350dp"). Please help! Thanks!

这是我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="none" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="#FF0000"
                android:gravity="center"
                android:text="Quick Links"
                android:textColor="@color/white"
                android:textSize="20sp" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <android.support.v4.view.ViewPager
                    android:id="@+id/pager"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >
                </android.support.v4.view.ViewPager>

                <com.viewpagerindicator.CirclePageIndicator
                    android:id="@+id/circlePage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/pager"
                    android:layout_marginBottom="10dp" >
                </com.viewpagerindicator.CirclePageIndicator>
            </RelativeLayout>

            <com.custom.ExpandableHeightListView
                android:id="@+id/lvAdmissions"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:divider="@drawable/bg_listview_divider"
                android:dividerHeight="1dp"
                android:listSelector="@drawable/selector_animation" >
            </com.custom.ExpandableHeightListView>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

已解决:我点击了此链接,它可以帮助我解决此问题. https://stackoverflow.com/a/24666987/1928560

Resolved: I follow this link and it help me resolved this issues. https://stackoverflow.com/a/24666987/1928560

推荐答案

ViewPager高度必须指定为 match_parent 或必须为其指定一个值.两种解决方案,

ViewPager height must either be specified match_parent or you must specify a value for it. Two solutions,

  1. LinearLayout 中使用 weights 概念或
  2. 在类文件中使用 onMeasure 方法.请参考 Android:我无法安装ViewPager WRAP_CONTENT
  1. Use weights concept inside LinearLayout or
  2. Use onMeasure method in your class file. Refer Android: I am unable to have ViewPager WRAP_CONTENT

这是在 LinearLayout

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#FF0000"
            android:gravity="center"
            android:text="Quick Links"
            android:textColor="@color/white"
            android:textSize="20sp" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </android.support.v4.view.ViewPager>

            <com.viewpagerindicator.CirclePageIndicator
                android:id="@+id/circlePage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/pager"
                android:layout_marginBottom="10dp" >
            </com.viewpagerindicator.CirclePageIndicator>
        </RelativeLayout>

        <com.custom.ExpandableHeightListView
            android:id="@+id/lvAdmissions"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:divider="@drawable/bg_listview_divider"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/selector_animation" >
        </com.custom.ExpandableHeightListView>
    </LinearLayout>

这样,您的viewpager布局和ExpandableListView共享相同的高度.您可以通过更改重量值来更改比例

This way your viewpager layout and ExpandableListView shares equal height. You can change the ratio by changing the weight value

这篇关于如果设置layout_height = wrap_content,则ViewPager无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆