OGeek|极客世界-中国程序员成长平台

标题: Android 工具栏中心标题和自定义字体 [打印本页]

作者: 菜鸟教程小白    时间: 2022-8-1 01:20
标题: Android 工具栏中心标题和自定义字体

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

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

有没有更好的方法来做到这一点?使用新的工具栏作为我的操作栏。



Best Answer-推荐答案


在您的 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4