【问题标题】:Why does ViewPager need to specify height manually?为什么 ViewPager 需要手动指定高度?
【发布时间】:2020-09-17 05:10:30
【问题描述】:

我有 NestedScrollView 包含 CardView、TabLayout 和 ViewPager。如果我指定 android:layout_height match_parent 或 wrap_content (只有选项卡名称可见),ViewPager 不会在选项卡上显示内容。它仅在我指定 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>

如何避免在数字中指定 android:layout_height 以使 ViewPager 将其与屏幕匹配为 Normal TabLayout?请帮忙

我尝试在 ViewPager 中添加我的 TabLayout 但不起作用(两者都变得不可见)

【问题讨论】:

    标签: android android-viewpager android-tablayout android-coordinatorlayout android-nestedscrollview


    【解决方案1】:

    您可以使用match_parent,并在ViewPager 中添加View,大小为math_parent

    喜欢:

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

    【讨论】:

    • 你能分享一个xml代码吗?因为我尝试了大多数对我不起作用的解决方案。
    • 您可以检查已编辑的内容,有时ViewPager 在子视图中无法正常工作。
    • 我已经应用了您的解决方案。我只能看到标签名称。我的片段不可见。
    • 我不确定确切的问题,但您是否考虑过使用约束布局?如果您在约束布局内正确约束它,此布局将允许您在视图寻呼机的宽度和高度上指定 0dp
    • 哦,我错过了,请将View设置为android:visibility="invisible"
    猜你喜欢
    • 1970-01-01
    • 2012-08-02
    • 2020-09-23
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    相关资源
    最近更新 更多