如何将自定义字体设置为android工具栏中的标题 [英] How to set a custom font to the title in toolbar android

查看:182
本文介绍了如何将自定义字体设置为android工具栏中的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  toolbar =(工具栏)findViewById(com.sports.unity.R.id.tool_bar) ; 
setSupportActionBar(toolbar);
setTitle(hello);

我想在标题hello中设置一个自定义字体如何做到这一点?为了在工具栏中使用自定义标题,您只需要记住工具栏只是一个奇特的ViewGroup,因此您可以添加自定义标题如下:

 < android.support.v7.widget.Toolbar 
android:id = @ b




android:layout_width =match_parent
android:minHeight =?attr / actionBarSize @ color / action_bar_bkgnd
app:theme =@ style / ToolBarTheme>

$ b< 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。所以在你的活动中,你可以像这样访问标题:

 工具栏toolbarTop =(工具栏)findViewById(R.id.toolbar_top) ; 
TextView mTitle =(TextView)toolbarTop.findViewById(R.id.toolbar_title);

然后:

 字体khandBold = Typeface.createFromAsset(BalrogApplication.getApplication()。getAssets(),fonts / Khand-bold.ttf); 

mTitle.setTypeface(khandBold);

更新
动态版本

  public static void changeToolbarFont(Toolbar toolbar,Activity context){
for(int i = 0; i< toolbar.getChildCount(); i ++ ){
View view = toolbar.getChildAt(i);
if(view instanceof TextView){
TextView tv =(TextView)view;
if(tv.getText()。equals(toolbar.getTitle())){
applyFont(tv,context);
break;




$ b public static void applyFont(TextView tv,Activity context){
tv.setTypeface(Typeface) createFromAsset(context.getAssets(),fonts / customFont));

code


并像这样使用

  changeToolbarFont(findViewById(R.id.app_bar),this); 


i am doing this

toolbar = (Toolbar) findViewById(com.sports.unity.R.id.tool_bar);
    setSupportActionBar(toolbar);
    setTitle("hello");

i want to set a custom font here in the title "hello" how to do that?

解决方案

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="?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>

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);

And then:

Typeface khandBold = Typeface.createFromAsset(BalrogApplication.getApplication().getAssets(), "fonts/Khand-bold.ttf");

mTitle.setTypeface(khandBold);

UPDATE dynamically version

public static void changeToolbarFont(Toolbar toolbar, Activity context) {
    for (int i = 0; i < toolbar.getChildCount(); i++) {
        View view = toolbar.getChildAt(i);
        if (view instanceof TextView) {
            TextView tv = (TextView) view;
            if (tv.getText().equals(toolbar.getTitle())) {
                applyFont(tv, context);
                break;
            }
        }
    }
}

public static void applyFont(TextView tv, Activity context) {
    tv.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/customFont"));
}

and use it like that

changeToolbarFont(findViewById(R.id.app_bar), this);

这篇关于如何将自定义字体设置为android工具栏中的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆