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

Android 如何更改操作栏/标题栏字体?,android,fonts,android-actionbar,Android,Fonts,Android Actionbar,不确定这是称为操作栏还是标题栏,但我需要将字体更改为资产文件夹中的字体。我该怎么做 您可以按如下方式访问工具栏的文本视图: TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title); <style name="baseStyle" parent="@style/Theme.MaterialComponents.Light.Bridge"> <ite

不确定这是称为操作栏还是标题栏,但我需要将字体更改为资产文件夹中的字体。我该怎么做


您可以按如下方式访问工具栏的文本视图:

TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
<style name="baseStyle" parent="@style/Theme.MaterialComponents.Light.Bridge">
    <item name="actionBarStyle">@style/myActionBarStyle</item>
  ...
</style>
然后指定所需的字体:

mTitle.setTypeface(someTypeface);

您可以按如下方式访问工具栏的文本视图:

TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
<style name="baseStyle" parent="@style/Theme.MaterialComponents.Light.Bridge">
    <item name="actionBarStyle">@style/myActionBarStyle</item>
  ...
</style>
然后指定所需的字体:

mTitle.setTypeface(someTypeface);
在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);
在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>
它会起作用的

编辑

在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);
在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>
它会起作用的

编辑


您可以创建自定义工具栏。下面是代码快照

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

然后,您可以访问java类活动或片段中的工具栏标题,并设置自定义字体样式。

您可以创建自定义工具栏。下面是代码快照

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

然后,您可以访问java类活动或片段中的工具栏标题,并设置自定义字体样式。

您可以尝试以下操作:-

ActionBar ActionBar=getSupportActionBar;
actionBar.setTitleNew标题

您可以尝试以下操作:-

ActionBar ActionBar=getSupportActionBar;
actionBar.setTitleNew标题

Android O引入了一个新功能,XML字体。现在,您可以创建自定义工具栏并使用

android:fontFamily="@font/lobster"

XML标记。您需要将字体添加到resources/font文件夹。有关更多信息,请参见Android O引入了一项新功能,即XML字体。现在,您可以创建自定义工具栏并使用

android:fontFamily="@font/lobster"

XML标记。您需要将字体添加到resources/font文件夹。有关更多信息,请参见

首先转到styles.xml并添加

字体名称


在主题标签中…

首先转到styles.xml并添加

字体名称


在您的主题标签中…

正如其他一些用户所建议的,您可以使用工具栏,但如果您不想或必须继续使用操作栏,则可以在您的样式中定义操作栏的样式,如下所示:

TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
<style name="baseStyle" parent="@style/Theme.MaterialComponents.Light.Bridge">
    <item name="actionBarStyle">@style/myActionBarStyle</item>
  ...
</style>

这只会影响操作栏中的字体,而不会影响整个应用程序中的字体

,正如其他一些用户所建议的那样,您可以使用工具栏,但如果您不想或必须继续使用操作栏,则可以按以下方式定义操作栏的样式:

TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
<style name="baseStyle" parent="@style/Theme.MaterialComponents.Light.Bridge">
    <item name="actionBarStyle">@style/myActionBarStyle</item>
  ...
</style>


这只会影响操作栏中的字体,而不会影响整个应用程序中的字体

如何访问工具栏变量?当我开始键入时,我的活动中不存在。是否使用工具栏或操作按钮?不确定。这是创建空活动时的默认设置。但我没有在xml或java中引用它;如何访问工具栏变量?当我开始键入时,我的活动中不存在。是否使用工具栏或操作按钮?不确定。这是创建空活动时的默认设置。但我没有在xml或java中引用它;这将在现有栏下添加另一个栏。我正在尝试编辑现有栏,该栏在您创建项目时作为默认栏显示。当我应用您给我的代码时,标题视图_2显示为红色,无法解析符号“标题视图_2”,请将布局名称指定为标题视图_2。这将在现有栏下添加另一个栏。我正在尝试编辑现有的条形图,该条形图在创建项目时是默认的。当我应用你给我的代码时,标题视图2显示为红色,无法解析符号“标题视图2”将布局名称命名为标题视图2。是否可以发布xml代码?是否可以发布xml代码?问题是关于自定义字体,但你写的是关于应用程序bat标题问题是关于自定义字体,但你们写的是关于应用程序bat标题。