Actionbar title font not changing in android

允我心安 提交于 2019-12-13 05:57:57

问题


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

回答1:


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>



回答2:


You can solve this problem by using this code

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



回答3:


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...




回答4:


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


来源:https://stackoverflow.com/questions/31176566/actionbar-title-font-not-changing-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!