Android 安卓工作室“;“底部导航活动”;模板在顶部留下空白区域

Android 安卓工作室“;“底部导航活动”;模板在顶部留下空白区域,android,android-jetpack,bottomnavigationview,Android,Android Jetpack,Bottomnavigationview,我通过选择“底部导航活动”创建了新的Android项目。没有其他变化。仅修改 “fragment_notifications.xml”,通过添加背景属性将背景设置为黑色 我的问题是: a) 为什么黑色的碎片不从蓝色旁边开始 标题区域,但顶部留1~2厘米空白?目的是什么 模板中此区域的名称?我觉得我没那么幸运能找到你 这样的错误很明显 如何消除空白区域。我试着修改 “activity_main.xml”和“fragment_notifications.xml”调整 宽度/高度和约束属性,但它们

我通过选择“底部导航活动”创建了新的Android项目。没有其他变化。仅修改 “fragment_notifications.xml”,通过添加背景属性将背景设置为黑色

我的问题是:

  • a) 为什么黑色的碎片不从蓝色旁边开始 标题区域,但顶部留1~2厘米空白?目的是什么 模板中此区域的名称?我觉得我没那么幸运能找到你 这样的错误很明显
  • 如何消除空白区域。我试着修改 “activity_main.xml”和“fragment_notifications.xml”调整 宽度/高度和约束属性,但它们都不起作用
提前谢谢你的时间

我正在使用Android Studio 4.1.1并在AVD Nexus 6 API 26上运行代码,下面是相关文件的源代码。除通知片段的背景外,所有内容均由Android Studio生成

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="0dp"
    android:layout_marginEnd="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottom_nav_menu" />

<fragment
    android:id="@+id/nav_host_fragment"
    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/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>
为什么黑色的片段不从蓝色标题区域旁边开始

原因是:
activity\u main.xml
根布局(
ConstraintLayout
)具有一个
paddingTop
属性,该属性添加了与
ActionBar
大小相等的顶部空格

android:paddingTop="?attr/actionBarSize"
ActionBar
size的高度等于56dp

<dimen name="abc_action_bar_default_height_material">56dp</dimen>
56dp
如何消除空白区域。

摆脱它

更新:

a) 这个填充的潜在用途是什么,为什么模板会留下这个填充?永远看不到标题栏下有操作栏的应用程序

根据您的设计,这可能会有基于意见的答案,但一般来说,您可以将其视为一个副标题的空间。。或者可能是因为
ActionBar
的标题宽度有限。。你可以找到一些设计指南和它的用途

b) 约束布局是此UI的根节点。但是为什么填充区不在标题区上方呢


这是因为根布局大小从默认操作栏的底部开始,该操作栏是使用
style.xml
中的应用程序主题设置的。。。您可以验证在根布局的最顶部添加
视图时。。此视图将直接显示在
操作栏下方,而不是在其后面。

尝试更改
文本视图的颜色

<TextView
android:id="@+id/text_notifications"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

看看你的
fragment\u notifications.xml
——你说的空白区域是文本视图
text\u notifications
,谢谢Zain。它完美地解决了这个问题。我仍然有两个顾虑:a)这个填充的潜在用途是什么,为什么模板会留下这个填充?永远不要看到标题栏下有操作栏的应用程序。b) 约束布局是此UI的根节点。但是为什么paddingTop不在标题区域上方?@gg感谢您的评论。。请检查更新的答案。。如果这对你有帮助,请接受回答对不起,我是新来的。我以前投过票,但不负责,因为我的信用不够。刚刚接受。这真的帮了我很多忙,为我节省了很多时间。一切都很清楚。欢迎来到SO社区,很高兴能帮上忙。。作为新来者,你的声誉可能还不足以提高投票率。。
android:paddingTop="?attr/actionBarSize"
<dimen name="abc_action_bar_default_height_material">56dp</dimen>
<TextView
android:id="@+id/text_notifications"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
getActivity().getSupportActionBar().hide();