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

发布于 2021-01-29 19:57:14

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

此刻,我使用的是旧的ActionBar,并且将标题设置为空值,并使用setCustomView自定义字体TextView并使用ActionBar.LayoutParams将其居中。

有更好的方法吗?使用新的工具栏作为我的ActionBar。

关注者
0
被浏览
496
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    要在您的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="?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。因此,在您的活动中,您可以像这样访问标题:

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


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看