Android 使用MaterialToolbar在AppBar布局中居中显示标题

Android 使用MaterialToolbar在AppBar布局中居中显示标题,android,material-design,android-toolbar,android-appbarlayout,Android,Material Design,Android Toolbar,Android Appbarlayout,我尝试在工具栏中将文本居中。我使用了中建议的AppBarLayout和MaterialToolbar 我尝试了在StackOverflow上找到的所有东西,以使标题居中,但似乎没有任何效果 这是我的工具栏: <com.google.android.material.appbar.AppBarLayout android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_heigh

我尝试在工具栏中将文本居中。我使用了中建议的
AppBarLayout
MaterialToolbar

我尝试了在StackOverflow上找到的所有东西,以使标题居中,但似乎没有任何效果

这是我的工具栏:

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/topAppBar"
        style="@style/CenteredToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />

</com.google.android.material.appbar.AppBarLayout>
然而,这不起作用


关于如何使标题居中,有什么建议吗?

不幸的是,我发现只有这种方法可以在工具栏中创建居中标题

奖励:您将完全控制工具栏的UI

创建自定义布局,将其用作工具栏,如下所示

<include layout="@layout/activity_action_bar" />
活动\动作\栏

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_preference"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal">

<TextView
    android:id="@+id/actionbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:maxLines="1"
    android:clickable="false"
    android:focusable="false"
    android:longClickable="false"
    android:fontFamily="@font/lato"
    android:textStyle="bold"
    android:textSize="20sp"
    android:textColor="@color/colorTextMain" />

</LinearLayout>
这将为您提供对工具栏的全面管理,例如居中标题或自定义UI


结果:

这是否回答了您的问题?不,没有。将文本视图放置到MaterialToolbar中会删除文本样式,并且我会丢失一些功能,例如根据底部导航上选定的选项卡设置标题(使用
setupActionBarWithNavController(navController,appBarConfiguration)
。尝试在您的样式中或直接在xml布局文件中使用foregroundGravity而不是gravity。这也没有任何作用。
<include layout="@layout/activity_action_bar" />
    setContentView(R.layout.activity_interface);
    // create and define a custom action bar
    View view = getLayoutInflater().inflate(R.layout.activity_action_bar, null);
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.MATCH_PARENT,
            Gravity.CENTER);
    TextView Title = view.findViewById(R.id.actionbar_title);
    Title.setText(getString(R.string.about));
    getSupportActionBar().setCustomView(view,params);
    getSupportActionBar().setDisplayShowCustomEnabled(true); // show custom title
    getSupportActionBar().setDisplayShowTitleEnabled(false); // hide the default title
    getSupportActionBar().setElevation(0);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_back);
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, R.color.colorPrimary))); // set background