将工具栏中的文本与图标对齐

3

这是我当前的工具栏外观。文字似乎完全错位。

输入图像描述

以下是代码:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="@color/blue_grey_900"
    android:gravity="center"
    android:minHeight="?android:attr/actionBarSize"
    app:popupTheme="@style/ToolbarPopup"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:elevation="5dp"/>

什么导致了错位?

你是如何添加返回图标的? - royB
@royB getSupportActionBar().setDisplayHomeAsUpEnabled(true);@royB getSupportActionBar().setDisplayHomeAsUpEnabled(true); - user4351462
2个回答

2

修改你的代码为android:layout_height="?attr/actionBarSize


不会改变任何东西。 - user4351462
你是否也更改了 minHeight?如果是,请展示一下 popupTheme 样式。 - royB
1
好的,我通过将layout_height更改为wrap_content和android:minHeight更改为"?attr/actionBarSize"来使其正常工作。 - user4351462
对我来说,问题是当layout_heightwrap_content且省略了minHeight时出现的。我将minHeight添加为?attr/actionBarSize,但没有任何效果。我能够完全省略minHeight,只需将layout_height更改为?attr/actionBarSize即可解决在设备旋转时对齐不良的问题。因此,原始答案恰好适合我。 - Rock Lee

0
您可以在XML中使用自定义的TextView,例如:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/blue_grey_900"
android:gravity="center"
android:minHeight="?android:attr/actionBarSize"
app:popupTheme="@style/ToolbarPopup"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:elevation="5dp">

  <TextView
    android:id="@+id/toolbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Title"
    android:layout_gravity="center"/>

</android.support.v7.widget.Toolbar>

然后您可以自定义它或将标题放置在任何您想要的位置。


如果只需要将文本视图与返回按钮对齐,特别是在重力位置方面,那么没有理由添加一个标题textView。他的问题涉及样式。 - royB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接