为什么 ViewPager Height match_parent 不起作用?

3 android android-layout android-fragments android-viewpager android-tablayout

我正在尝试使用 ViewPager 的 match_parent 高度来替换片段。但layout_height =“match_parent”或“wrap_content”不起作用。如果我使用它们,那么我的观点就会变得不可见。

为什么它仅在我在 dp 中指定时才有效?我如何使其匹配_parent。

我的 XML 布局代码:这是 Fragment Layout 替换为 FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Fragments.Frag.MyFragment">

        <com.google.android.material.tabs.TabLayout
            android:background="@drawable/toolbar"
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <androidx.viewpager.widget.ViewPager
        android:background="#f1f1f1"
        android:id="@+id/Viewpager"
        android:layout_width="match_parent"
        android:layout_height="600dp"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我的 MainActivity将上面的 XML 替换为 FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white_light2"
    android:fitsSystemWindows="true">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                app:layout_scrollFlags="scroll|enterAlways"
                style="@style/customToolBar"
                android:id="@+id/toolbar1"
                app:title="Home">
            </androidx.appcompat.widget.Toolbar>
        </com.google.android.material.appbar.AppBarLayout>

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

        <FrameLayout
            android:layout_marginBottom="?attr/actionBarSize"
            android:id="@+id/FragmentHolder"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.drawerlayout.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

我得到的输出为

我的代码输出,ViewPager 没有展开

如何避免在 dp 中指定 ViewPager 高度来占据整个屏幕空间(match_parent)

小智 5

您只需android:fillViewport="true"在 NestedScrollView 标记中添加属性即可!