Android 使用BottomNavigationView和jetpack时,将工具栏标题居中 主屏幕(首次加载)
Android 使用BottomNavigationView和jetpack时,将工具栏标题居中 主屏幕(首次加载),android,toolbar,bottomnavigationview,Android,Toolbar,Bottomnavigationview,从底部导航导航到第二个选项 为了使每个选定屏幕/片段的标题在操作栏上居中(使用BottomNavigationView时),我创建了自定义工具栏以隐藏默认标题并将其显示在工具栏的中心,代码如下所示 XML部分 <?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://sch
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".bkk_revamp.home_screen.HomeScreenActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="@color/white"
android:paddingBottom="15dp">
<TextView
android:id="@+id/title_toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:textColor="@color/white"
android:layout_gravity="center_horizontal"/>
</androidx.appcompat.widget.Toolbar>
<androidx.appcompat.widget.SearchView
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:background="@drawable/search_box"
android:iconifiedByDefault="false"
app:layout_constraintBottom_toBottomOf="@+id/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar">
<requestFocus />
</androidx.appcompat.widget.SearchView>
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/search"
app:navGraph="@navigation/app_navigation" />
<!--android:layout_height="wrap_content"-->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#F5F5F5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/drawer_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemIconTint="@color/colorPrimary"
app:menu="@menu/drawer_navigation_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
它在应用程序加载时运行良好,但当从底部导航单击另一个选项时,默认屏幕标签也开始出现,如何仅显示一个标题并位于工具栏的中心。我想您已经尝试过了
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
同样的结果,仍然得到双标题名称
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");