Android 工具栏中心标题和自定义字体

标签 android android-layout android-toolbar android-theme android-styles

我正在尝试找出使用自定义字体作为工具栏标题的正确方法,并将其置于工具栏中(客户要求)。

目前,我正在使用旧的 ActionBar,我将标题设置为空值,并使用 setCustomView 放置我的自定义字体 TextView 并使用 ActionBar.LayoutParams 将其居中。

有没有更好的方法来做到这一点?使用新的 Toolbar 作为我的 ActionBar。

最佳答案

要在 Toolbar 中使用自定义标题,您只需记住 Toolbar 只是一个精美的 ViewGroup,因此您可以添加自定义标题,如下所示:

<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。因此,在您的 Activity 中,您可以像这样访问标题:

Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);

关于Android 工具栏中心标题和自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26533510/

相关文章:

android - 通过复制一些数据 android 重新创建一个 firebase 对象

android - 如何在 RecyclerView 中使用 fastScrollEnabled?

安卓可点击布局

java - Android ListView 显示为空,但包含对象

android - 如何将所有操作项放在左侧,尽可能多地占用空间,同时在右侧溢出?

android - 是否所有智能手机浏览器(Android 和 iOS)都支持多点触控/手势?

android - 如何将 Google Play Developer API 版本更新到 v3?

android - 如何在没有填充的情况下在android中获得准确的命中矩形?

Android SearchView 小部件 : setIconified[ByDefault or not](false) not working

Android 支持工具栏样式