Android:如何在工具栏中将标题居中

编程入门 行业动态 更新时间:2024-10-28 13:27:45
本文介绍了Android:如何在工具栏中将标题居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我第一次在项目中使用ToolBar,所以我不知道如何在android中自定义工具栏.我需要将标题居中放置在工具栏上,如何操作请告诉我.

I am using ToolBar in my project first time, so i do not know how to customized the toolbar in android. I need to centered title in to the tool bar and how to do that please tell me.

谢谢.

推荐答案

在工具栏对齐的中心简单地添加TextView的问题是在工具栏中添加了菜单项,这些菜单项将抵消居中的文本.

The problem with simply adding a TextView in the Toolbar aligned center is adding menu items in the toolbar which will offset the centered text.

要解决此问题,我将文本分层放置在工具栏的顶部,而不是内部.这样,无论您添加多少图标,都不会偏移居中的文本:

To get around this, I've layered the text on top of the Toolbar, not inside it. This way it doesn't matter how many icons you add, it will not offset the centered text:

<android.support.design.widget.AppBarLayout android:id="@+id/appbar_layout" android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.ActionBar"> </android.support.v7.widget.Toolbar> <TextView android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name"/> </RelativeLayout> </android.support.design.widget.AppBarLayout>

通过这种方式,不需要任何额外的逻辑来补偿后退按钮/溢出菜单/搜索图标等在工具栏上的偏移距离,因为居中的文本位于其上方,而不位于其中.

This way there is no need for any extra logic to compensate for the offset spacing of back buttons/overflow menu/search icons etc. on the toolbar, because the centered text is above it, not in it.

更多推荐

Android:如何在工具栏中将标题居中

本文发布于:2023-10-07 22:21:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1470678.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   工具栏   标题   如何在   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!