[英]How to center the title in a toolbar (even with toolbar back button displayed)
[英]How to center toolbar back button?
我在尝试将支持工具栏上的后退按钮居中时遇到问题。 我在 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
actionBarSize 的自定义值设置为 40dp,但是,现在事实证明,这是标题放错了位置。
Toolbar 支持比 ActionBar 更集中的功能集。 从头到尾,工具栏可能包含以下可选元素的组合:
一个导航按钮。 这可能是向上箭头、导航菜单切换、关闭、折叠、完成或应用程序选择的其他字形。 此按钮应始终用于访问工具栏容器内的其他导航目的地及其指定内容,或者以其他方式离开工具栏指定的当前上下文。 导航按钮在工具栏的最小高度内垂直对齐(如果设置)。
因此,如果您将minHeight属性设置为与工具栏高度 ( layout_height ) 相同,则后退箭头将垂直居中。
如果您有非标准的工具栏高度,现在使用androidx
Toolbar
将后退按钮 (2k20) androidx
您只需使用buttonGravity
属性即可。
这允许您使用wrap_content
而不是特定的高度( minHeight
不允许您这样做):
<androidx.appcompat.widget.Toolbar
app:buttonGravity="center_vertical"
android:layout_height="wrap_content"
... />
内部 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">
请记住,工具栏只是视图组,因此您可以按照自己喜欢的任何方式设置样式。 这是图片链接:工具栏示例
希望能帮助到你。
实现此目的的最佳方法是从工具栏中删除常规的后退按钮,并在您的 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>
捎带@Rick Sanchez 的回答- 如果您知道工具栏的minHeight
属性的大小,则可以使用:
android:gravity="top"
app:titleMarginTop="@dimen/margin_to_center"
在工具栏中使文本居中。 如果您使用的是android:minHeight="?actionBarSize"
那么这大约是 13p
对我来说,没有一个解决方案有效。 就我而言,问题是我申请的保证金:
<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>
@Rick Sanchez 回答是可行的,将 Toolbar 的 layout_height 设置为与 minHeight 相同
我发现在 Toolbar 构造函数中有一个字段可以设置按钮重力, mButtonGravity = a.getInteger(R.styleable.Toolbar_buttonGravity, Gravity.TOP);
,但v7支持Toolbar无法设置, mButtonGravity = Gravity.TOP;
让你的按钮大小56dp
和集scaleType
到center
与无边距Appbar
确保你的图标是24x24
尺寸
我正在使用
<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
当我应该使用
<style name="MyActionBar" parent="@style/Widget.AppCompat.Toolbar">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.