在新的材质设计中,是否像流行的Android应用程序一样,在工具栏上有居中标题的官方API?

and*_*per 11 android material-design android-toolbar

背景

过去,Google始终显示工具栏以使其标题与左侧对齐:

https://material.io/develop/android/components/app-bar-layout/

但是,最近看来,即使在其某些应用程序中,标题居中,即使左右两侧没有对称内容。示例在“消息”应用程序上

在此处输入图片说明

“ Google新闻”应用上

在此处输入图片说明

此处也对材料指南进行了更新。例:

在此处输入图片说明

有些人喜欢将其称为“材料设计2.0”,因为它对各种事物进行了更新。

问题

自从它被称为“ ActionBar”(此处为示例)以来,在StackOverflow上存在很多类似的问题,但这里有所不同,因为它应该具有支持库(又名“ Android-X”)来正确处理它,因为情况已经改变,现在Google应该支持它,因为它是指南的一部分,也是各种Android应用程序的一部分。

我尝试过的

我试图在工具栏中添加视图,但是即使通过给视图上色,您也可以看到它并没有自动居中,而是在添加更多操作项或向上按钮之前:

在此处输入图片说明

activity_main.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout
        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=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay">

            <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="title"
                      android:gravity="center" android:background="#33ff0000"
                      android:id="@+id/titleTextView"/>
        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

此解决方案与在其他与该问题类似的其他StackOverflow线程上介绍的其他解决方案相似,并且就像在此处一样,它无法真正使文本居中,而在Google的尝试下,无论操作项和其他视图的数量如何,它都能正确居中周围。

我还想出了一个更好的解决方法:在每一侧都有一个工具栏,并使TextView的余量是两者的最大值。甚至还有一个库,“ Toolbar-Center-Title ” ...但是,这也是一种解决方法。不是我要问的。

问题

现在有没有一种正式的方法可以在工具栏上居中显示标题,而不管有多少个动作项目,而又在另一面是什么(例如,向上按钮)?

如果是这样,该怎么办?

注意:同样,我对解决方法不感兴趣。有很多解决方法,我自己可以想到。

Nil*_*hod 2

现在我们有一种使用 Material Design 3 将标题置于工具栏中央的官方方法

使用 Material Design 3,我们可以将标题对齐在中心,而无需做额外的工作或无需在工具栏中添加文本视图

为了使标题居中对齐,我们需要正确使用以下内容

 app:titleCentered="true"
Run Code Online (Sandbox Code Playgroud)

为了将字幕居中对齐,我们需要使用以下属性

app:subtitleCentered="true"
Run Code Online (Sandbox Code Playgroud)

示例代码

<com.google.android.material.appbar.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/topAppBar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:title="Center Aligned title"
        app:subtitle="Sub title"
        app:subtitleCentered="true"
        app:menu="@menu/top_app_bar"
        app:titleCentered="true"
        app:navigationIcon="@drawable/ic_android_black_24dp" />

</com.google.android.material.appbar.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)

输出

在此输入图像描述