Android Studio“底部导航活动”模板在顶部留下空白区域

g g*_*g g 6 android bottomnavigationview android-jetpack

我通过选择 \xe2\x80\x9cBottom Navigation Activity\xe2\x80\x9d 创建了新的 Android 项目。没有其他改变。仅修改\n\xe2\x80\x9cfragment_notifications.xml\xe2\x80\x9d 通过添加背景属性将背景设置为黑色。

\n

截屏

\n

我的问题是:

\n
    \n
  • a) 为什么黑色的片段不是从蓝色标题区域的旁边开始,而是在顶部留下1~2厘米的空白?模板中此区域的用途是什么?我不认为我这么“幸运”地发现了这样明显的错误。
  • \n
  • b) 如何消除空白区域。我尝试修改\n“activity_main.xml”和“fragment_notifications.xml”调整\n宽度/高度和约束属性,但它们都不起作用。
  • \n
\n

感谢您提前抽出时间。

\n

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

\n

活动_main.xml:

\n
<?xml version="1.0" encoding="utf-8"?>\n<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"\n    xmlns:app="http://schemas.android.com/apk/res-auto"\n    android:id="@+id/container"\n    android:layout_width="match_parent"\n    android:layout_height="match_parent"\n    android:paddingTop="?attr/actionBarSize">\n\n<com.google.android.material.bottomnavigation.BottomNavigationView\n    android:id="@+id/nav_view"\n    android:layout_width="0dp"\n    android:layout_height="wrap_content"\n    android:layout_marginStart="0dp"\n    android:layout_marginEnd="0dp"\n    android:background="?android:attr/windowBackground"\n    app:layout_constraintBottom_toBottomOf="parent"\n    app:layout_constraintLeft_toLeftOf="parent"\n    app:layout_constraintRight_toRightOf="parent"\n    app:menu="@menu/bottom_nav_menu" />\n\n<fragment\n    android:id="@+id/nav_host_fragment"\n    android:name="androidx.navigation.fragment.NavHostFragment"\n    android:layout_width="match_parent"\n    android:layout_height="match_parent"\n    app:defaultNavHost="true"\n    app:layout_constraintBottom_toTopOf="@id/nav_view"\n    app:layout_constraintLeft_toLeftOf="parent"\n    app:layout_constraintRight_toRightOf="parent"\n    app:layout_constraintTop_toTopOf="parent"\n    app:navGraph="@navigation/mobile_navigation" />\n\n</androidx.constraintlayout.widget.ConstraintLayout>\n
Run Code Online (Sandbox Code Playgroud)\n

mobile_navigation.xml如下:

\n
<?xml version="1.0" encoding="utf-8"?>\n<navigation xmlns:android="http://schemas.android.com/apk/res/android"\n    xmlns:app="http://schemas.android.com/apk/res-auto"\n    xmlns:tools="http://schemas.android.com/tools"\n    android:id="@+id/mobile_navigation"\n    app:startDestination="@+id/navigation_home">\n\n<fragment\n    android:id="@+id/navigation_home"\n    android:name="com.guo.butnavtest.ui.home.HomeFragment"\n    android:label="@string/title_home"\n    tools:layout="@layout/fragment_home" />\n\n<fragment\n    android:id="@+id/navigation_dashboard"\n    android:name="com.guo.butnavtest.ui.dashboard.DashboardFragment"\n    android:label="@string/title_dashboard"\n    tools:layout="@layout/fragment_dashboard" />\n\n<fragment\n    android:id="@+id/navigation_notifications"\n    android:name="com.guo.butnavtest.ui.notifications.NotificationsFragment"\n    android:label="@string/title_notifications"\n    tools:layout="@layout/fragment_notifications" />\n
Run Code Online (Sandbox Code Playgroud)\n\n

fragment_notifications.xml如下:

\n
<?xml version="1.0" encoding="utf-8"?>\n<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"\n    xmlns:app="http://schemas.android.com/apk/res-auto"\n    xmlns:tools="http://schemas.android.com/tools"\n    android:layout_width="match_parent"\n    android:layout_height="match_parent"\n    android:background="@color/black"\n    tools:context=".ui.notifications.NotificationsFragment">\n\n<TextView\n    android:id="@+id/text_notifications"\n    android:layout_width="match_parent"\n    android:layout_height="wrap_content"\n    android:layout_marginStart="8dp"\n    android:layout_marginTop="8dp"\n    android:layout_marginEnd="8dp"\n    android:textAlignment="center"\n    android:textSize="20sp"\n    app:layout_constraintBottom_toBottomOf="parent"\n    app:layout_constraintEnd_toEndOf="parent"\n    app:layout_constraintStart_toStartOf="parent"\n    app:layout_constraintTop_toTopOf="parent" />\n</androidx.constraintlayout.widget.ConstraintLayout>\n
Run Code Online (Sandbox Code Playgroud)\n

MainActivity.java如下:

\n
public class MainActivity extends AppCompatActivity {\n\n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.activity_main);\n    BottomNavigationView navView = findViewById(R.id.nav_view);\n    // Passing each menu ID as a set of Ids because each\n    // menu should be considered as top level destinations.\n    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(\n            R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)\n            .build();\n    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);\n    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);\n    NavigationUI.setupWithNavController(navView, navController);\n}\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Zai*_*ain 5

为什么黑色片段没有从蓝色标题区域旁边开始

原因:activity_main.xml根布局 ( ConstraintLayout) 有一个paddingTop属性,可以添加顶部空白区域,该空白区域的大小等于ActionBar

android:paddingTop="?attr/actionBarSize"
Run Code Online (Sandbox Code Playgroud)

size的高度ActionBar等于56dp

<dimen name="abc_action_bar_default_height_material">56dp</dimen>
Run Code Online (Sandbox Code Playgroud)

如何消除空白区域。

摆脱它。

更新:

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

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

b)约束布局是该UI的根节点。但为什么 paddingTop 不在标题区域上方?

这是因为根布局大小从使用应用程序主题设置的默认 底部开始......您可以验证当您在根布局的最顶部添加一个时,..该视图将被布局直接在其下方而不是在其后面。ActionBarstyles.xmlViewActionBar