ScrollView 中 ViewPager 的 Match Parent 属性无效

5
我试图将View Pager放在表格行中,我将View Pager布局高度设置为match parent(撑满父容器),结果View Pager没有任何高度。我必须以dp为单位设置布局高度才能使View Pager有高度。但是,我正在View Pager中充气的内容是动态的,因此我无法在xml中设置高度。ScrollView已经填满了屏幕,但ViewPager没有任何高度。有什么办法可以解决这个问题吗?(有问题的ViewPager具有id pager_problem)
编辑:
好的,现在我更改后,View Pager存在了,但只有与屏幕一样大小。如果Fragment的内容很长,我想使View Pager中的内容可滚动(让整个布局都可滚动,而不仅是View Pager中的Fragment)。
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.emilsjolander.components.StickyScrollViewItems.StickyScrollView
        android:layout_width="match_parent"
        android:fillViewport="true"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">
            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <TableRow>
                    <android.support.v4.view.ViewPager
                        android:id="@+id/pager_image"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="100dp" />
                </TableRow>
                <TableRow android:tag="sticky">
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="50dp"
                        android:id="@+id/tab"
                        android:tag="sticky"
                        android:layout_below="@id/pager_image"
                        android:orientation="horizontal">

                        <LinearLayout
                            android:id="@+id/frame_general_tab"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center">
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textSize="15dp"
                                android:text="GENERAL"/>
                        </LinearLayout>
                        <LinearLayout
                            android:id="@+id/frame_detail_tab"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center"
                            android:background="@color/gray_F2">
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textSize="15dp"
                                android:text="DETAIL"/>
                        </LinearLayout>

                        <LinearLayout
                            android:id="@+id/frame_foto_tab"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center"
                            android:background="@color/gray_F2">
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textSize="15dp"
                                android:text="FOTO"/>
                        </LinearLayout>
                    </LinearLayout>
                </TableRow>
                <TableRow>
                    <!--<LinearLayout android:id="@+id/page_container"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:orientation="vertical"
                        android:layout_marginBottom="70dp"
                        android:layout_height="match_parent">
                    </LinearLayout>-->
                    <android.support.v4.view.ViewPager
                        android:id="@+id/pager"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_marginBottom="70dp"
                        android:layout_height="match_parent"/>

                </TableRow>
            </TableLayout>


        </LinearLayout>
    </com.emilsjolander.components.StickyScrollViewItems.StickyScrollView>

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

        <GridLayout
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            android:columnCount="2"
            android:background="@color/gray_F2"
            android:rowCount="3"
            android:orientation="vertical"

            android:layout_height="70dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:textSize="15dp"
                android:layout_gravity="right"
                android:textColor="@color/gray_72"
                android:text="Harga mulai"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:textSize="20dp"
                android:layout_gravity="right"
                android:text="Rp 10.560.000"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:textSize="15dp"
                android:layout_gravity="right"
                android:textColor="@color/gray_72"
                android:text="per kamar per malam"/>

            <Button android:layout_width="150dp"
                android:layout_marginLeft="20dp"
                android:layout_height="match_parent"
                android:layout_rowSpan="3"
                android:background="@color/traveloka_button_orange"
                android:textColor="@color/white"
                android:layout_margin="10dp"
                android:text="LIHAT KAMAR"/>

        </GridLayout>
    </RelativeLayout>
</RelativeLayout>
1个回答

17

你需要在scrollview中设置 android:fillViewport="true"

android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent"

请使用以下代码来设置ScrollView的高度:android:layout_height="match_parent" - SHASHIDHAR MANCHUKONDA
已经尝试了,但还是不行。问题并不在ScrollView上,ScrollView似乎已经填满了屏幕。但ViewPager没有任何高度。 - stephen1706
好的,现在我修改后,ViewPager 存在了,但是只有屏幕那么大,如果 Fragment 内容很长,我想让 ViewPager 中的内容可以滚动(整个布局都可以滚动,而不仅仅是 ViewPager 中的 Fragment)。 - stephen1706
我曾经遇到了一个问题,就是ScrollView不能填满整个屏幕,然后通过添加“android:fillViewport =”true“”解决了这个问题。 - brunoapimentel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接