Android 工具栏中心标题和自定义字体 [英] Android toolbar center title and custom font
问题描述
我正在尝试找出为工具栏标题使用自定义字体的正确方法,并将其置于工具栏的中心(客户要求).
I'm trying to figure out the right way to use a custom font for the toolbar title, and center it in the toolbar (client requirement).
目前,我正在使用旧的 ActionBar,我将标题设置为空值,并使用 setCustomView
放置我的自定义字体 TextView 并使用 ActionBar.LayoutParams 将其居中.
At the moment, i'm using the good old ActionBar, and I was setting the title to empty value, and using setCustomView
to put my custom font TextView and center it using ActionBar.LayoutParams.
有没有更好的方法来做到这一点?使用新的工具栏作为我的操作栏.
Is there a better way to do that? Using the new Toolbar as my ActionBar.
推荐答案
要在 Toolbar
中使用自定义标题,您需要做的就是记住 Toolbar
是只是一个花哨的 ViewGroup,因此您可以像这样添加自定义标题:
To use a custom title in your Toolbar
all you need to do is remember is that Toolbar
is just a fancy ViewGroup so you can add a custom title like so:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?android:attr/actionBarSize"
android:background="@color/action_bar_bkgnd"
app:theme="@style/ToolBarTheme" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:layout_gravity="center"
android:id="@+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
这意味着您可以随意设置 TextView
的样式,因为它只是一个常规的 TextView
.因此,在您的活动中,您可以像这样访问标题:
This means that you can style the TextView
however you would like because it's just a regular TextView
. So in your activity you can access the title like so:
Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);
这篇关于Android 工具栏中心标题和自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!