【问题标题】:Toolbar align title in center with back button工具栏将标题与后退按钮居中对齐
【发布时间】:2016-02-11 11:40:57
【问题描述】:

我在我的应用程序中添加了Toolbar,但我似乎遇到了问题。在应用程序中,我应该在屏幕右侧有一个按钮,并且标题在中间。但是当我深入应用程序时,我应该通过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>

【问题讨论】:

  • 只是一些解决方法,但一个简短且耗时的想法是在布局中添加后退箭头,自行隐藏、显示和控制行为。
  • @Amy 是的,但是,这是一个好习惯。我正在寻找一个应该很好的解决方案,以便我将来可以使用它,它也会对其他人有所帮助
  • 如果没有其他可能,这可能是一个很好的做法。否则,这只是一个快速修复的想法。 :)
  • @RaguSwaminathan 仍然没有
  • 是否有机会包含一些屏幕截图来准确显示问题所在?

标签: android android-actionbar android-xml android-toolbar


【解决方案1】:

对我有用的是设置:

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

到工具栏的子视图,它对我来说是一个线性布局(对你来说是一个相对布局)。一开始对我来说这听起来很奇怪,但它确实有效,所以我不会抱怨。

【讨论】:

  • 绝对不是这个问题的答案
【解决方案2】:

首先,一些一般性提示。这就是 Android Hierarchical view 背后的确切目的。使用它来确定后退按钮的容器对象是什么,所有内容如何组合在一起等等。

我怀疑您可以通过将部分wrap_content 更改为match_parent 来解决您的问题。具体来说,我怀疑在您的RelativeLayout 中将其更改为layout_width 可以解决问题。但即使不是这样,也可以使用 Hierarchical 视图更好地了解您在玩耍时发生的确切情况,它应该有助于为您指明正确的方向。

【讨论】:

    【解决方案3】:

    您可能设置了活动标题,这会阻止徽标位于操作栏的中心。

    试试:

    getActionBar().setDisplayShowTitleEnabled(false);

    对于 v.7:

    getSupportActionBar().setDisplayShowTitleEnabled(false);

    这来自this question and answer。感谢@Fillipo Mazza。

    希望对你有帮助。

    【讨论】:

      【解决方案4】:

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

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

          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" />
      

      此文本视图将附加在工具栏布局内,并在工具栏布局的中心对齐。即使您添加返回键或操作菜单,它也应保持在中心。

      【讨论】:

      • 我已经尝试过这种方法,但它对我不起作用(它左对齐)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 2012-06-08
      • 2023-03-06
      • 2017-03-19
      • 2018-07-30
      • 1970-01-01
      • 2022-11-19
      相关资源
      最近更新 更多