在新材料设计中,是否有工具栏上的中心标题的官方API,如流行的Android应用程序?

问题描述 投票:9回答:3

背景

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

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

然而,最近,似乎在某些应用程序中,标题是居中的,即使它在左右两侧没有对称内容。示例在"Messages" app上:

enter image description here

"Google News" app

enter image description here

它还获得了材料指南here的最新信息。例:

enter image description here

有些人喜欢称它为“材料设计2.0”,因为它更新了各种各样的东西。

问题

虽然在StackOverflow被称为“ActionBar”(例如here)时有很多类似的问题,但这里有不同之处,因为它应该有支持库(AKA“Android-X”)有办法正确处理它,因为事情已经发生了变化,现在谷歌应该支持它,因为它是指南的一部分,也是各种Android应用程序的一部分。

我试过的

我试图在工具栏中添加一个视图,但即使通过着色视图,您也可以看到它没有真正居中,自动,并且在添加更多动作项或上行按钮之前:

enter image description here

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>

这个解决方案类似于其他类似于这个问题的其他StackOverflow线程上的其他解决方案,就像那里一样,它无法真正使文本居中,而在Google的尝试中,无论操作项和其他视图的数量如何,它都能正确居中周围。

我还提出了一个更好的解决方法:每边都有一个工具栏,并使TextView的边距是两者的最大值。甚至有一个图书馆,“Toolbar-Center-Title”......但是,这是一个解决方法。不是我要问的。

这个问题

现在有一种正式的方法可以在工具栏上设置一个居中的标题,无论有多少动作项,无论另一方是什么(例如,按钮)?

如果是这样,怎么办呢?

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

android material-design android-toolbar
3个回答
2
投票

TL; DR:不,目前还没有正式的方法可以将标题置于工具栏上。


我认为没有正式的办法,至少现在还没有。但我知道Flutter框架支持它并且它非常直接:你只需要将centerTitle: true传递给appbar构造函数,如answer中所述。您在问题中提到的应用很可能是使用Flutter构建的,因为两者都来自Google。

我认为与预期布局最接近的解决方法是在工具栏顶部放置TextView,如图所示here

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
        </android.support.v7.widget.Toolbar>

        <TextView
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"/>

    </RelativeLayout>
</android.support.design.widget.AppBarLayout>

但是,如果官方的Android API能够以同样的方式支持它,那将是非常好的。也许您想向Material Components Android Issue Tracker发送功能请求?


0
投票

textAlignment="center"怎么样?当RelativeLayout设置了AppCompatTextView时,layout_width="match_parent"就可以了。例如:

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/customView"
        android:minHeight="?android:attr/actionBarSize"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:gravity="top">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <!-- Home Button -->
            <include
                layout="@layout/button_home_menu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start"/>

            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="8dp">

                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/app_name"
                    android:textAlignment="center"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/subtitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/app_version"
                    android:textAlignment="center"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textSize="12sp"
                    android:textStyle="bold"/>

            </androidx.appcompat.widget.LinearLayoutCompat>

        </RelativeLayout>

    </androidx.appcompat.widget.LinearLayoutCompat>

</layout>

在下方是,当将多个菜单项显示为动作按钮时 - 或者当将非常长的字符串显示为标题时,标题可能会重叠 - 但是当仅显示一个或两个动作按钮与视觉上适合的标题时可用宽度,这工作得很好 - 这是因为菜单配置ifRoom总是适用,因为有空间。否则只能测量工具栏的哪一侧有最宽项目的容器 - 然后调整另一侧项目容器的宽度。根据可用空间缩放字体大小也可能是一个选项,以使其动态适应。


0
投票

没有官方的方法,但子类化提供了大多数报道没有疯狂的技巧。

https://gist.github.com/bmc08gt/40a151e93969f2633b9b92bca4b31e83

© www.soinside.com 2019 - 2024. All rights reserved.