【发布时间】:2021-04-28 11:05:51
【问题描述】:
目前,我正在使用来自 Android 的 Navigation 组件,并且操作栏标题显示在左侧。但是,我有点希望它集中化以使事情更整洁,有什么办法可以做到吗?
Image of the actionbar title that is displayed on the left
活动代码:
public class General extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
private NavController navController;
private AppBarConfiguration appBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.general);
bottomNavigationView = findViewById(R.id.bottomNavigationView);
navController = Navigation.findNavController(this, R.id.fragment);
appBarConfiguration = new AppBarConfiguration.Builder(R.id.addFragment, R.id.homeFragment).build();
NavigationUI.setupWithNavController(bottomNavigationView, navController);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
}
}
活动的 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".General">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_navigation_menu">
</com.google.android.material.bottomnavigation.BottomNavigationView>
<fragment
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/the_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
片段 XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/the_navigation"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.test.HomeFragment"
android:label="Home"
tools:layout="@layout/home_fragment" />
<fragment
android:id="@+id/addFragment"
android:name="com.test.AddFragment"
android:label="Add"
tools:layout="@layout/add_fragment" />
</navigation>
【问题讨论】:
-
这能回答你的问题吗? Android toolbar center title and custom font
-
如果我要使用工具栏,请您指出正确的方向吗?如果您包含有关如何将我的代码与工具栏集成的代码 sn-ps,我将不胜感激:D
-
official docs 是一个很好的起点
-
尝试向活动添加自定义工具栏,然后尝试上面提供的链接。另外,您需要在样式中添加
<item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>。
标签: android android-fragments android-actionbar androidx