【问题标题】:Center title in AppBarLayout with MaterialToolbar使用 MaterialToolbar 在 AppBarLayout 中居中标题
【发布时间】:2020-08-26 21:57:29
【问题描述】:

我尝试将工具栏中的文本居中。我使用AppBarLayoutMaterialToolbar,正如Material Design 中所建议的那样。

我尝试了在 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>

CenteredToolbar 是我尝试将标题居中的自定义样式:

<style name="CenteredToolbar" parent="Widget.MaterialComponents.Toolbar.Primary">
    <item name="android:gravity">center_horizontal</item>
</style>

但是,这不起作用。

关于如何使标题居中的任何建议?

【问题讨论】:

  • 这能回答你的问题吗? Android: How to Center title in ToolBar
  • 不,它没有。将 TextView 放入 MaterialToolbar 会删除文本样式,并且我失去了根据底部导航上的选定选项卡设置标题等功能(使用 setupActionBarWithNavController(navController, appBarConfiguration).
  • 尝试在您的样式中或直接在您的 xml 布局文件中使用 foregroundGravity 而不是重力。
  • 那也没什么作用。

标签: android material-design android-toolbar android-appbarlayout


【解决方案1】:

我在这里分享了我的答案。来自com.google.android.material:material:1.4.0app:titleCentered="true"

https://stackoverflow.com/a/69589951/2810726

【讨论】:

    【解决方案2】:

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

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

    创建一个自定义布局,将其用作这样的工具栏。

    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>
    

    在您的界面布局中,XML 包含这样的工具栏。

    <include layout="@layout/activity_action_bar" />
    

    现在在你 setContentView 的类中使用之前的界面布局执行此操作。

        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
    

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

    结果:

    【讨论】:

    • 在材质工具栏中将文本居中时,我仍然遇到同样的问题。当您只显示一个边缘附件(向上导航/溢出菜单)时,材质工具栏会做一些愚蠢的事情。不敢相信 Google 还没有解决这个问题。
    • @angryITguy 他们的举动很简单:他们弃用并且不再建议使用导航菜单。
    • @angryITguy 这已经提供了stackoverflow.com/a/69589962/2810726
    【解决方案3】:

    这就是我解决问题的方法。对我来说很好。我使用了材料设计主题。

    <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"
            style="@style/Widget.MaterialComponents.Toolbar.Surface"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:menu="@menu/top_app_bar"
            app:navigationIcon="@drawable/ic_baseline_arrow_back_24">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textAppearance="?attr/textAppearanceHeadline6"
                android:text="Manali" />
        </com.google.android.material.appbar.MaterialToolbar>
    
    </com.google.android.material.appbar.AppBarLayout>
    

    【讨论】:

    • 看起来不错看演示。
    • 它实际上并没有居中。当您在左侧显示向上导航并隐藏溢出菜单时,也会出现问题。开发人员需要独立于这些额外位使文本居中
    • @angryITguy,你应该给出不同的解决方案,因为我没有找到其他解决方案。只是在我所有的项目中使用这个解决方案。
    • 我愿意,但我现在没有时间投资。但重要的是 SO 用户知道它没有居中。 (我当时用的是Android 8.1,后续版本可能会有所不同。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 2018-01-02
    • 2017-07-16
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多