简体   繁体   English

如何更改操作栏/标题栏字体?

[英]How to change Action Bar/Title Bar font?

Not sure if this is called the action bar or the title bar but I need to change the font to a font I have in my assets folder.不确定这是否称为操作栏或标题栏,但我需要将字体更改为资产文件夹中的字体。 How do I do this?我该怎么做呢?

在此处输入图像描述

As some other users have suggested, you can use a toolbar, but if you don't want to or you have to keep using the action bar, then you can define the style of the action bar in your style like this:正如其他一些用户所建议的那样,您可以使用工具栏,但如果您不想或必须继续使用操作栏,那么您可以像这样在您的样式中定义操作栏的样式:

<style name="baseStyle" parent="@style/Theme.MaterialComponents.Light.Bridge">
    <item name="actionBarStyle">@style/myActionBarStyle</item>
  ...
</style>

and inside myActionBarStyle define the font:并在myActionBarStyle定义字体:

  <style name="myActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    <item name="fontFamily">@font/customFont</item>
    ...
  </style>

this will affect the font only in the action bar and not in the whole app这只会影响操作栏中的字体,而不会影响整个应用程序

You can make a custom toolbar.您可以制作自定义工具栏。 below is code snapshot下面是代码快照

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay">

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textColor="@color/white"
        android:textSize="17sp"
        android:textStyle="bold" />
</android.support.v7.widget.Toolbar>

and then you can access toolbar_title in your java class(Activity or Fragment) and set a custom font style.然后您可以访问 java 类(活动或片段)中的 toolbar_title 并设置自定义字体样式。

You can access textview of your toolbar like this:您可以像这样访问工具栏的文本视图:

TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);

And then assign needed font:然后分配所需的字体:

mTitle.setTypeface(someTypeface);

In java code在java代码中

 ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2B1C27")));

LayoutInflater inflater = LayoutInflater.from(this);
View v = inflater.inflate(R.layout.titleview_2, null);

TextView actionbar_title = (TextView)v.findViewById(R.id.actionbar_title);
actionbar_title.setTypeface(you can set your font here also.);

actionBar.setCustomView(v);

in XML file在 XML 文件中

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

It will work.它会起作用。

EDITED已编辑

getSupportActionBar().setTitle((Html.fromHtml("<font face=\"Times New Roman\">" + getString(R.string.title) + "</font>")));

First Go to styles.xml and the add首先转到styles.xml并添加

Font Name字体名称

In Your Theme Tag...在您的主题标签中...

If you want to do it programmatically in java then you can paste the below code in OnCreate method in your activity.如果您想在 java 中以编程方式执行此操作,则可以将以下代码粘贴到活动的 OnCreate 方法中。 It works for the later api versions.它适用于以后的 api 版本。

        Typeface typeface = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            typeface = getResources().getFont(R.font.lilitaoneregular);
        }
        SpannableString str = new SpannableString("Daily Khabar");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            str.setSpan(new TypefaceSpan(typeface),0, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        actionBar.setTitle(str);

You can try the following :-您可以尝试以下操作:-

ActionBar actionBar=getSupportActionBar(); ActionBar actionBar=getSupportActionBar();
actionBar.setTitle("New Title"); actionBar.setTitle("新标题");

"Android O introduces a new feature, Fonts in XML". “Android O 引入了一项新功能,XML 中的字体”。 You now can make a custom toolbar and use the您现在可以制作自定义工具栏并使用

android:fontFamily="@font/lobster"

XML tag. XML 标记。 You need to add your font to the recources/font folder.您需要将字体添加到资源/字体文件夹中。 For more info see https://developer.android.com/preview/features/working-with-fonts.html有关更多信息,请参阅https://developer.android.com/preview/features/working-with-fonts.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM