【问题标题】:Why does ViewPager Height match_parent Not Working?为什么 ViewPager 高度 match_parent 不起作用?
【发布时间】:2020-09-23 09:23:22
【问题描述】:

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

为什么它只在我在 dp 中指定时才有效?我如何使它成为 match_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>

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>

我得到的输出

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

【问题讨论】:

  • 你试过零dp吗
  • 是的。但没有效果。我需要在协调器布局中包装我的片段 xml 代码吗?

标签: android android-layout android-fragments android-viewpager android-tablayout


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你应该这样做

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

    【讨论】:

    • 这对我不起作用。我看不到 ViewPager 中的视图。我得到的效果就像我在 match_parent 或 wrap_content 中指定时一样。
    • 听起来问题出在您的 PagerAdapter 上。
    • 你是在同一个xml中实现一个drawer和一个fragment吗?
    • 片段是不同的布局。替换为 MainActivity 的 FrameLayout。
    • 听起来像有 ViewPager 的 Fragment 应该把 childFragmentManager 给 ViewPager 的 PagerAdapter
    【解决方案3】:

    所以我已经检查过了,我能够看到全宽和全高的框架布局。或许能解决你的问题。

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

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      相关资源
      最近更新 更多