gpt4 book ai didi

android - 为什么 ViewPager 高度 match_parent 不起作用?

转载 作者:行者123 更新时间:2023-12-05 00:19:39 25 4
gpt4 key购买 nike

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

为什么它只在我在 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>

我的主要 Activity 将上面的 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:fillViewport="true"您的 NestedScrollView 标记中的属性!

关于android - 为什么 ViewPager 高度 match_parent 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62193842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com