工具栏如何将标题与返回按钮居中对齐

16

我正在将工具栏添加到我的应用程序中,但似乎出了问题。在应用程序中,我应该有一个位于屏幕右侧的按钮,以及中心的标题。但是当我在应用程序中深入时,我应该在左侧显示返回箭头,通过执行getSupportActionBar()。setDisplayHomeAsUpEnabled()来实现。这个可以正常工作,但是当我添加返回按钮时,文本会向右移动,以便返回按钮可以有空间。请问有人能告诉我如何让我的按钮在右侧,并且标题始终处于中心位置,无论是否显示返回按钮?

这是我的工具栏xml布局代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:padding="5dp"
    tools:ignore="MissingPrefix">

    <RelativeLayout
        android:id="@+id/toolbar_info_holder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:textColor="@color/actionbar_title_color" />

        <ImageView
            android:id="@+id/toolbar_image"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/toolbar_count"
            android:layout_width="13dp"
            android:layout_height="13dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/red_circle"
            android:gravity="center"
            android:text="4"
            android:textColor="@color/white"
            android:textSize="9sp" />
    </RelativeLayout>

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

1
只是一些解决方法,但一个简短而不费话的想法是在您的布局中添加后退箭头,并通过自己隐藏、显示和控制行为来实现。 - yennsarah
1
@Amy 是的,但这是一个好的做法吗?我正在寻找一个好的解决方案,这样我将来可以使用它,并且它也会帮助其他人。 - Darko Petkovski
如果没有其他选择,这可能是一个好的做法。否则,这只是一个快速修复的想法。 :) - yennsarah
@RaguSwaminathan 仍然没有。 - Darko Petkovski
1
有没有可能包含一些截图,准确地展示问题所在? - PearsonArtPhoto
4个回答

3

我的解决方案是设置:

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"

对于我而言,工具栏的子视图是一个LinearLayout(对于你而言则是RelativeLayout),这听起来有点奇怪,但它能够正常工作,所以我不会抱怨。


绝对不是这个问题的答案。 - josemigallas

0

首先,一些通用的提示。这就是 Android 分层视图 的确切目的。使用它来找出返回按钮的容器对象是什么,如何将所有内容组合在一起等。

我怀疑你可以通过将一些 wrap_content 更改为 match_parent 来在一定程度上解决问题。具体来说,我认为在 RelativeLayout 中更改 layout_width 将会奏效。但即使如此,使用分层视图来更好地理解当你进行操作时发生了什么,它应该有助于指引你朝着正确的方向前进。


0

您可能已经设置了活动标题,这会防止徽标在操作栏上居中显示。

尝试:

getActionBar().setDisplayShowTitleEnabled(false);

对于版本7:

getSupportActionBar().setDisplayShowTitleEnabled(false);

这来自于这个问题和答案。感谢 @Fillipo Mazza 的贡献。

希望对您有所帮助。


0

这篇文章有点晚了,但我会解释它是如何工作的。

您可以使用以下代码为工具栏使用自定义视图。

    supportActionBar?.setCustomView(R.layout.app_bar_title)
    supportActionBar?.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM

基本上发生的事情是,在工具栏内添加了app_bar_layout作为自定义视图。 假设需要在中心显示标题,并在右侧显示返回键或操作按钮。 我们可以创建这样的布局。
<?xml version="1.0" encoding="utf-8"?>
<TextView 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:id="@+id/app_bar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_gravity="center"
    tools:text="HOME" />

这个TextView将附加在Toolbar布局内,并在Toolbar布局的中心对齐。即使您添加了返回键或操作菜单,它也应保持在中心位置。


我尝试过这种方法,但对我没有用(它是左对齐的)。 - ade.se

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