为什么ViewPager Height match_parent不起作用?

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

我正在尝试使用match_parent高度作为ViewPager的片段。但是layout_height =“ match_parent”或“ wrap_content”都不起作用。如果我使用它们,那么我的视图将不可见。

为什么只有在dp中指定时才起作用?如何使其与match_parent匹配。

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

<?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>

My 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>

我得到的输出

My output of code, ViewPager not expanding

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

android android-layout android-fragments android-viewpager android-tablayout
2个回答
0
投票

您应该这样做

<?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="0dp"
        android:layout_weight="1"/> <!-- here -->

</LinearLayout>

0
投票

所以我已经检查了一下,并且能够看到具有完整宽度和高度的框架布局。可能会解决您的问题。

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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
                android:id="@+id/toolbar1"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"
                app:title="Home" />
        </com.google.android.material.appbar.AppBarLayout>

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

            <FrameLayout
                android:id="@+id/FragmentHolder"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/black"
                android:backgroundTint="@color/black" />
        </androidx.core.widget.NestedScrollView>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.drawerlayout.widget.DrawerLayout>
© www.soinside.com 2019 - 2024. All rights reserved.