为什么ViewPager需要手动指定高度?

问题描述 投票:0回答:1

我有 NestedScrollView 包含CardView,TabLayout和ViewPager,如果我指定android:layout_height或者match_parent或者wrap_content,ViewPager不显示Tab上的内容。如果我指定了android:layout_height或者match_parent或者wrap_content,ViewPager就不能显示Tabs上的内容(只有tab名称可见)。只有当我在dp中指定值时,它才会工作。

我的XML布局如下。

<androidx.core.widget.NestedScrollView
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
                <TextView
                    android:layout_marginTop="10dp"
                    android:id="@+id/DescriptionTv"
                    android:textStyle="bold"
                    android:text="Post description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </LinearLayout>

        </androidx.cardview.widget.CardView>

                <com.google.android.material.tabs.TabLayout
                    app:tabGravity="fill"
                    android:id="@+id/TabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                </com.google.android.material.tabs.TabLayout>

            <androidx.viewpager.widget.ViewPager
                android:id="@+id/Viewpager"
                android:layout_width="match_parent"
                android:layout_height="500dp"/>  // I want to avoid specifying values but not working
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

我如何避免在ViewPager的数字中指定android:layout_height,使其与屏幕匹配为普通TabLayout?请帮助

我已经尝试在ViewPager中添加我的TabLayout,但无法工作(两者都变得不可见)。

android android-viewpager android-tablayout android-coordinatorlayout android-nestedscrollview
1个回答
0
投票

你可以使用 match_parent 并拥有 View 里面 ViewPager 规模 math_parent.

喜欢。

<ViewPager ...layout_height="match_parent">
   <View android:layout_width="match_parent" 
         android:layout_height="match_parent" />
</ViewPager>

© www.soinside.com 2019 - 2024. All rights reserved.