android - 为什么 fragment_container 控件在 Android Studio 中会填满整个屏幕?
问题描述
有两个控件,两者nav_view
和fragment_container
。
我希望它nav_view
位于屏幕底部,并fragment_container
填充空闲空间,所以我写了代码A。
但是我发现fragment_container
填满了整个屏幕,你可以看到Image 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
intofragment
标签正在占用整个屏幕(因为您的导航栏不可见)。因为ConstarintLayout
您需要使用match_constraints
match_parent 来实现您想要的。Hello World 在他的评论中是完美的。
使用下面的代码
<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" />
推荐阅读
- javascript - PHP弹出警报
- github - 如何将 github 存储库链接到 linode 服务器?
- javascript - 如何缩短 if/else 语句
- angular - Angular:组件内包含的模态窗口渲染
- apache-spark - Pyspark - 当列包含特定字符串时如何对列求和
- java - JPA 符合条件的前一行
- lamp - Bitnami LAMP 堆栈 .htmaccess 不工作
- c# - Automapper 忽略 null 值,但将空字符串映射到 null
- regex - 在 Hive 中用空替换单引号(')
- android - 如何发送 Zebra GX420d 打印机以使用 Dart 进行打印,或者如何配置 Android 的 IP?