【问题标题】:How to center toolbar back button?如何使工具栏后退按钮居中?
【发布时间】:2015-07-05 14:49:59
【问题描述】:

我在尝试将支持工具栏上的后退按钮居中时遇到问题。 我在 ActionBarActivity 中使用它。

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:toolbar="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    toolbar:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    toolbar:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

并在 Activity 的onCreate() 中设置导航,如下所示:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

getSupportActionBar().setTitle(R.string.title_activity_scanner);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

但是,我得到的是:

如您所见,后退按钮放错了位置

编辑: 问题似乎在于将 ?attr/actionBarSize 的自定义值设置为 40dp,但现在发现,它是放错位置的标题。

【问题讨论】:

  • 我认为禁用该后退按钮并将按钮小部件放入其中并根据需要对齐的操作之一。希望有帮助!!!
  • 你能去掉 minHeight 标签并将高度添加为“?attr/actionBarSize”而不是“wrap_content”
  • @Viren 我想尽可能避免重新发现轮子
  • @RiyazAhamed 请检查更新的问题,实际上是标题使工具栏变大,但后退按钮也应该适应

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


【解决方案1】:

(2020 年)

如果你有一个非标准的工具栏高度,现在(2k20)用androidxToolbar将后退按钮居中,你可以使用buttonGravity属性。

这允许您使用wrap_content 而不是特定的高度(minHeight 不允许您这样做):

<androidx.appcompat.widget.Toolbar
   app:buttonGravity="center_vertical"
   android:layout_height="wrap_content"
   ... />

【讨论】:

  • 哇,谢谢!我想知道这是否有最低 API 版本?
  • @LuckMan 我没有看到工具栏本身的任何具体要求,我认为可以安全地假设androidx.widget.* 在 API 14+ (Android 4.0) 上工作——AFAIK,这就是要求的 pre-androix 支持库。
【解决方案2】:

我正在使用

  <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">

我应该在什么时候使用

 <style name="MyActionBar" parent="@style/Widget.AppCompat.Toolbar">

【讨论】:

    【解决方案3】:

    使您的按钮大小为56dp,并将scaleType 设置为center,Appbar 上没有边距 确保您的图标是24x24 尺寸

    【讨论】:

      【解决方案4】:

      @Rick Sanchez 回答是可行的,设置工具栏的 layout_height 与 minHeight 相同

      我在 Toolbar 构造函数中发现有一个可以设置按钮重力的字段, mButtonGravity = a.getInteger(R.styleable.Toolbar_buttonGravity, Gravity.TOP);

      ,但是v7支持工具栏无法设置, mButtonGravity = Gravity.TOP;

      【讨论】:

        【解决方案5】:

        搭载@Rick Sanchez answer - 如果您知道工具栏的minHeight 属性的大小,则可以使用:

        android:gravity="top"
        app:titleMarginTop="@dimen/margin_to_center"
        

        在工具栏中使文本居中。如果您使用的是android:minHeight="?actionBarSize",那么这将是大约 13p

        【讨论】:

          【解决方案6】:

          对我来说,没有一个解决方案有效。就我而言,问题是我应用的边距:

           <android.support.v7.widget.Toolbar
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               ... >
          
               <View
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginBottom="10dp"
                  android:layout_marginTop="10dp" />
          
           </android.support.v7.widget.Toolbar>
          

          直接应用到工具栏后,按钮垂直居中:

           <android.support.v7.widget.Toolbar
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:layout_marginBottom="10dp"
               android:layout_marginTop="10dp"
               ... >
          
               <View
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" />
          
           </android.support.v7.widget.Toolbar>
          

          【讨论】:

            【解决方案7】:

            来自developer.android.com:

            Toolbar 支持比 ActionBar 更集中的功能集。从一开始 最后,工具栏可能包含以下可选的组合 元素:

            一个导航按钮。这可能是向上箭头、导航菜单切换、 关闭、折叠、完成或应用程序选择的其他字形。这 按钮应始终用于访问其他导航目的地 在工具栏的容器内及其所指的内容或 否则离开工具栏表示的当前上下文。 该 导航按钮在工具栏的最小值内垂直对齐 高度(如果已设置)。

            因此,如果您将 minHeight 属性设置为与工具栏高度(layout_height )相同,则后退箭头将垂直居中。

            【讨论】:

            • 感谢您的简短而聪明的回答
            • 我使用的是自定义toolbar:theme,所以我的问题通过从我的toolbar's theme 中删除&lt;item name="android:actionBarSize"&gt;@android:/abc_action_bar_stacked_max_height&lt;/item&gt; 解决了
            • 如果使用支持24之前的v7,我们可以设置maxButtonHeight等于android:minHeight 做一个垂直居中的效果。 v7版本>=24+之后,可以设置attrbuttonGravity。
            • 必须添加android:minHeight="@null" 以取消来自工具栏样式style="@style/Widget.MaterialComponents.Toolbar" 的minHeight
            • 你拯救了我的一天。谢谢
            【解决方案8】:

            ActionBarActivity 内部

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
              setSupportActionBar(toolbar);
              getSupportActionBat().setTitle("Hello World"); 
              getSupportActionBar().setDisplayHomeAsUpEnabled(true);
              getSupportActionBar().setHomeButtonEnabled(true);
            

            布局代码:

            <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@color/primary"
                    app:theme="@style/ToolbarTheme"
                    app:popupTheme="@style/Theme.AppCompat"/>
            

            和风格:

             <style name="AppTheme.Base" parent="Theme.AppCompat.Light">
                <item name="colorPrimary">@color/primary</item>
                <item name="colorPrimaryDark">@color/primaryDark</item>
                <item name="android:windowNoTitle">true</item>
                <item name="android:windowActionBar">false</item>
                <item name="windowActionBar">false</item>
            </style>
            
            <style name="AppTheme" parent="AppTheme.Base">
            

            记住工具栏只是视图组,所以你可以用你喜欢的任何方式来设置它的样式。 这是图片链接:Toolbar sample

            希望对你有帮助。

            【讨论】:

              【解决方案9】:

              实现这一点的最佳方法是从工具栏中删除常规的后退按钮,并在您的 XML 布局中添加一个自定义 ImageButton 并为其设置图像。您可以将此 ImageButton 放置在工具栏上的任意位置。

              您的活动布局代码应该是这样的。

              <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
              
                  <include
                      android:id="@+id/toolbar"
                      layout="@layout/toolbar" />
              
                  <ImageButton
                      android:layout_width="60dp"
                      android:layout_height="60dp"
                      android:id="@+id/button"
                      android:src="@drawable/attach_icon"
                      android:layout_alignParentTop="true"
                      android:layout_centerHorizontal="true" />
              
              </RelativeLayout>
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-09-13
                • 1970-01-01
                • 1970-01-01
                • 2013-03-28
                相关资源
                最近更新 更多