【问题标题】:Change font of default ActionBar更改默认 ActionBar 的字体
【发布时间】:2017-04-26 19:25:56
【问题描述】:

我想更改我的 ActionBar 标题的字体。我找到了很多关于此搜索的结果(example1example2example3example4),但任何结果都可以解决我的问题。

我想更改没有任何可以调用的 TextView 项的默认 ActionBar 的字体并修改字体。我知道我可以创建自定义 ActionBar,但我想尝试在没有自定义 ActionBar 的情况下执行此操作。

这是我的代码:

MainActivity.java

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

ma​​in1.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>

ActionBar 结果:

我能做什么?

谢谢大家。

【问题讨论】:

  • 如果你想改变 Action bar 的标题,只需在你的 Activity getActionBar().setTitle("Action Bar Title"); 的 onCreate() 中提供以下 1 行代码,以防你使用支持库。使用:getSupportActionBar().setTitle("Action Bar Title");跨度>
  • 我不想改变标题而是标题的字体
  • stackoverflow.com/a/15181195/5829494 看看这个可能对你有帮助。我试过了,它有效。只需将字体放在 main/assets/fonts 中即可
  • 当我应用最后一个解决方案时,出现错误 Cannot resolve contructor 'TypefaceSpan(MainActivity, java.lang.string) in this part s.setSpan(new TypefaceSpan(this, "Yanone.ttf"), 0, s.length()。我能做什么?
  • 你已经关闭了TypefaceSpan 类,对吧?

标签: android android-actionbar


【解决方案1】:

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

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

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

【讨论】:

  • 我没有研究过这个解决方案,但我还有一个问题:什么更好?这个解决方案还是创建一个自定义的ActionBar?
  • 使用自定义视图来操作栏,你可以做任何你想做的事——添加自定义布局,添加任何视图等等。使用样式你只能改变外观(颜色、文本样式、背景)
  • 好的。另一个问题:您在评论中输入的代码是与书法库一起使用还是不使用?
  • 您可以在没有书法的情况下使用它。但是,如果需要,您可以在以后简单地添加,只需在 MyCustomTextStyleWithFont 中添加 fontPath。
  • 你检查过书法图书馆吗?当我尝试添加依赖项并同步 Gradle 文件时出现此错误:Error:(3) Error retrieving parent for item: No resource found that match the given name 'android:TextAppearance.Material.Widget.Button.逆'。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-17
  • 2016-04-10
  • 1970-01-01
相关资源
最近更新 更多