【发布时间】: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