【问题标题】:How do you change the typeface of a title of a toolbar?如何更改工具栏标题的字体?
【发布时间】:2019-01-02 11:05:14
【问题描述】:
我正在用 Java 中的 Android Studio 制作一个 Android 应用程序。我的应用程序将有一个工具栏。我想将我的工具栏标题的字体更改为字体。
字体不是 Android Studio 已经提供的字体,所以我使用了本教程。 https://www.youtube.com/watch?v=fB17m3kX-go
但我想尝试更改工具栏标题的字体,但没有办法这样做。我尝试使用 setTextAppearance 但我不知道如何使用 styles.xml 资源项来更改它。
【问题讨论】:
标签:
android
android-toolbar
【解决方案1】:
将字体文件粘贴到res/font 文件夹中。
如果font 子文件夹不存在,您必须创建它,
通过右键单击res 并选择New>Android Resource Directory,
然后将名称设置为font。
在styles.xml 中为您的工具栏创建一个主题:
<style name="MyToolbarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:fontFamily">@font/myfont</item>
</style>
最后在工具栏的 xml 中添加这个属性:
android:theme="@style/MyToolbarTheme"
【解决方案2】:
在 xml 的工具栏中有一个textview。
然后使用setTypeface() 更改该文本视图的字体。
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Title Here"
android:layout_gravity="center"
android:id="@+id/title" />
</android.support.v7.widget.Toolbar>
设置字体为title
在您的活动中,
TextView toobar_title = findViewById(R.id.title);
toolbar_title.setTypeface(your_typeface);
【解决方案3】:
工具栏是一个视图组。所以你可以把任何你想要的视图放在那里。然后您可以根据需要修改这些视图