Actionbar title font not changing in android

2019-09-16 20:15发布

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

4条回答
叼着烟拽天下
2楼-- · 2019-09-16 20:18

You can solve this problem by using this code

this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
查看更多
对你真心纯属浪费
3楼-- · 2019-09-16 20:30

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

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>
查看更多
不美不萌又怎样
4楼-- · 2019-09-16 20:37

use

getActivity().getActionBar().setCustomView(myTextView);

in that view you can use TextView for which you can set Font of your choice

TextView myTextView = new TextView(context);        
      myTextView.setTypeface(font);
查看更多
霸刀☆藐视天下
5楼-- · 2019-09-16 20:39

you can make your own custom action bar and then change the title font. and you can put your font file in assets folder and access from it. Try like this:

tf= Typeface.createFromAsset(getAssets(), "FontType.TTF");

 actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#9B66AB")));

    LayoutInflater inflater = LayoutInflater.from(c);
    @SuppressLint("InflateParams")
    View v = inflater.inflate(R.layout.titleview_2, null);

    TextView actionbar_title = (TextView)v.findViewById(R.id.actionbar_title);
    actionbar_title.setTypeface(tf);

    actionBar.setCustomView(v);

and it's xml file will be like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/actionBar"
android:paddingTop="7dp"
android:orientation="horizontal"
android:background="#9B66AB">

<TextView
    android:id="@+id/actionbar_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|center_vertical"
    android:gravity="center_horizontal|center_vertical"
    android:padding="8dp"
    android:text="@string/appTitle"
    android:textSize="20sp"
    android:textColor="#FFFFFF"
    android:layout_centerVertical="true" />
</RelativeLayout>

Hope it helps...

查看更多
登录 后发表回答