微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以使用 AndroidX 导航将操作栏标题从片段居中?

如何解决是否可以使用 AndroidX 导航将操作栏标题从片段居中?

目前,我使用的是 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>

解决方法

经过一番搜索后,似乎 this 成功了。我所要做的就是将其添加到我的活动代码中。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。