有两个控件,分别为nav_view
和fragment_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
使用match_parent
into fragment
标记会占据整个屏幕(因为导航栏不可见)。因为ConstarintLayout
您需要使用match_constraints
match_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" />
谢谢!您的代码运行良好,但是有一个名为的关键字
match_constraints
吗?没有0dp
意思match_constraints
?@HelloCW是的。match_constraints的值为0。有关更多信息,请访问developer.android.com/training/…