android - 如果设置 layout_height = wrap_content,ViewPager 不工作

标签 android

我在使用 ViewPaper 时遇到问题。 当我设置 ViewPaper 的 height = wrap_content 时它没有显示任何内容,如果我为 ViewPager 设置高度它会显示(例如:android:layout_height= "350dp")。请帮忙! 谢谢!

这是我的 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

最佳答案

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

  1. LinearLayout 中使用 weights 概念 或
  2. 在类文件中使用onMeasure 方法。引用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 共享相同的高度。可以通过改变权重值来改变比例

关于android - 如果设置 layout_height = wrap_content,ViewPager 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30884837/

相关文章:

javascript - 在 Chrome Android 中获取 BLOB

android - 多状态切换按钮

android - 异步任务内存泄漏

java - Android:在单击按钮时获取 Spinner 的 Textview 值

android - 在 RecyclerView 中获取可见项目

java - : Boolean method 'methodName' is always inverted 的 SuppressWarnings

android - TextInputLayout 中 float 提示中的不透明背景

android - 自定义相机实现

java - Android 中的线程和进度条?

android - 在android中的 View 上绘制和缩放自定义矩形