Actionbar标题字体在Android中未更改 [英] Actionbar title font not changing in android

查看:139
本文介绍了Actionbar标题字体在Android中未更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将自定义字体更改为我的操作栏标题,已经经历了几个公认的答案,但是没有运气,我尝试如下操作,但是我的自定义字体未应用于我的操作栏标题,请帮助我摆脱这个,我的代码如下:

I want to change a custom font to my action bar title, have gone through couple of accepted answers,but no luck,I have tried as below,But my custom font is not applied to my actionbar title,Please help me to get rid of this,my code is as below:

java

Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
                "neuropolx.ttf");
        SpannableString s = new SpannableString(
                "                     KOLAY RANDEVU");
        s.setSpan(new CustomTypefaceSpan("", font), 0, 4,
                Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        getActivity().getActionBar().setTitle(s);

推荐答案

不完全支持更改Actionbar标题字体,但是您可以对操作栏使用自定义视图.使用自定义视图并禁用本机标题.它将显示在图标和动作项目之间.您可以从一个活动继承所有活动,该活动在onCreate中具有以下代码:

Changing Actionbar title font is not completely supported, but you can use a custom view for your action bar. Use custom view and disable native title. It will display between icon and action items. You can inherit all activities from a single activity, which has this code in onCreate:

this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);

LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.customTitleView, null);

//you can customise anything about the text here.
//Using a custom TextView with a custom font in layout xml so all you need to do is set title
((TextView)v.findViewById(R.id.title)).setText(this.getTitle());

//assign the view to the actionbar
this.getActionBar().setCustomView(v);

和您的布局xml(上面的代码中为R.layout.customTitleView)如下所示:

and your layout xml (R.layout.customTitleView in the code above) looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent" >

<com.your.package.CustomTextView
        android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:textSize="20dp"
            android:maxLines="1"
            android:ellipsize="end"
            android:text="" />
</LinearLayout>

这篇关于Actionbar标题字体在Android中未更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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