温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Why does the fragment_container control fill in whole screen in Android Studio?
android android-studio

其他 - 为什么fragment_container控件会在Android Studio中填满整个屏幕?

发布于 2020-03-28 23:29:14

有两个控件,分别为nav_viewfragment_container

我希望它nav_view位于屏幕底部,并fragment_container填写可用空间,因此我编写代码A。

但是我发现fragment_container填满整个屏幕,可以看到图像A,该如何解决?

代码A

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="40dp"

            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent">

    </LinearLayout>


    <fragment
            android:id="@+id/fragment_container"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"

            app:layout_constraintBottom_toTopOf="@id/nav_view"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"

            app:navGraph="@navigation/nav_graph" />


</androidx.constraintlayout.widget.ConstraintLayout>

图片A

在此处输入图片说明

查看更多

查看更多

提问者
HelloCW
被浏览
73
Pankaj Kumar 2020-01-31 17:40

使用match_parentinto fragment标记会占据整个屏幕(因为导航栏不可见)。因为ConstarintLayout您需要使用match_constraintsmatch_parent来实现您想要的。你好世界在他的评论中是完美的。

使用以下代码

<fragment
            android:id="@+id/fragment_container"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:defaultNavHost="true"

            app:layout_constraintBottom_toTopOf="@id/nav_view"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"

            app:navGraph="@navigation/nav_graph" />