【发布时间】:2020-08-26 21:57:29
【问题描述】:
我尝试将工具栏中的文本居中。我使用AppBarLayout 和MaterialToolbar,正如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