更改默认ActionBar的字体

3
我想改变我的ActionBar标题的字体。我已经找到了很多关于此搜索的结果 (example1, example2, example3, example4),但是没有一个能解决我的问题。
我想要改变默认ActionBar的字体,它没有任何可以调用和修改字体的TextView项。我知道我有可能创建一个自定义ActionBar,但我想尝试在不使用自定义ActionBar的情况下完成这个任务。
这是我的代码:

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);
    ...

main1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/mapa"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>

操作栏结果:

enter image description here

我能做什么?

谢谢一切。


我不想改变标题,只想改变标题的字体。 - Iratzar Carrasson Bores
看看这个,它可能会对你有所帮助。我尝试过了,它可以工作。只需将字体放在main/assets/fonts中即可。 - sanidhya pal
当我应用最后一个解决方案时,在这部分s.setSpan(new TypefaceSpan(this, "Yanone.ttf"), 0, s.length()中出现错误“无法解析构造函数'TypefaceSpan(MainActivity,java.lang.string)'”。我该怎么办? - Iratzar Carrasson Bores
是的!但它仍然无法执行。 - Iratzar Carrasson Bores
你能展示给我你正在编写的代码吗? - sanidhya pal
显示剩余8条评论
1个回答

1

要更改textview的字体,您可以使用 Calligraphy库

如果您想要在没有自定义视图的情况下更改ActionBar外观,则可以使用样式。对于AppTheme,您可以设置自定义actionBarStyle。

    <style name="MyCustomActionBarStyle" parent="@style/Widget.AppCompat.ActionBar">
        <item name="android:titleTextStyle">@style/MyCustomTextStyleWithFont</item>
    </style>

我还没有尝试过这个解决方案,但我有另一个问题:哪个更好?这个解决方案还是创建一个自定义的ActionBar? - Iratzar Carrasson Bores
1
使用自定义视图来替代actionbar,您可以随心所欲地添加任何自定义布局、视图等。而仅使用样式,只能更改外观(颜色、文本样式、背景)。 - garmax1
好的。另一个问题:您在评论中放置的代码是使用了Calligraphy库还是没有使用它? - Iratzar Carrasson Bores
1
你可以不使用书法字体,但如果需要,只需在MyCustomTextStyleWithFont中添加fontPath即可。 - garmax1
你有检查过书法库吗?当我尝试添加依赖并同步Gradle文件时,出现了以下错误:Error:(3) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse'. - Iratzar Carrasson Bores
你的错误不适用于Calligraphy。我使用Calligraphy,它可以正常工作。 - garmax1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接