有没有办法改变布局中所有textview的字体?
目前我正在使用它来手动更改字体.
TextView txtAppName = (TextView) findViewById(R.id.txtAppName);
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/Existence-Light.otf");
txtAppName.setTypeface(tf);
Run Code Online (Sandbox Code Playgroud)
编辑:我已编辑问题以澄清我的问题.
也许对你来说有点晚了,但对于其他Android用户来说这可能非常有用.
不幸的是,Android并没有提供快速,简单和干净的方式来改变整个应用程序的字体.但是最近我调查了这个问题并创建了一些工具,允许你在没有任何编码的情况下更改字体(你可以通过xml,样式甚至文本外观来完成).您可以在此博客上阅读所有相关内容,并在此处查看github项目.
以下是如何应用这些工具的示例.将所有字体文件放入assets/fonts/.然后,在xml文件中声明这些字体并在应用程序的早期加载此文件TypefaceManager.initialize(this, R.xml.fonts);(例如,在Application类的onCreate中).xml文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<familyset>
<!-- Some Font. Can be referenced with 'someFont' or 'aspergit' -->
<family>
<nameset>
<name>aspergit</name>
<name>someFont</name>
</nameset>
<fileset>
<file>Aspergit.ttf</file>
<file>Aspergit Bold.ttf</file>
<file>Aspergit Italic.ttf</file>
<file>Aspergit Bold Italic.ttf</file>
</fileset>
</family>
<!-- Another Font. Can be referenced with 'anotherFont' or 'bodoni' -->
<family>
<nameset>
<name>bodoni</name>
<name>anotherFont</name>
</nameset>
<fileset>
<file>BodoniFLF-Roman.ttf</file>
<file>BodoniFLF-Bold.ttf</file>
</fileset>
</family>
</familyset>
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用flFont属性在样式或xml中使用它们.以下是将字体应用于整个应用中的所有文本的方法:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<!-- Application theme -->
<!-- Use a different parent if you don't want Holo Light -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:textViewStyle">@style/MyTextViewStyle</item>
</style>
<!-- Style to use for ALL text views (including FontTextView) -->
<!-- Use a different parent if you don't want Holo Light -->
<style name="MyTextViewStyle" parent="@android:style/Widget.Holo.Light.TextView">
<item name="android:textAppearance">@style/MyTextAppearance</item>
</style>
<!-- Text appearance to use for ALL text views (including FontTextView) -->
<!-- Use a different parent if you don't want Holo Light -->
<style name="MyTextAppearance" parent="@android:style/TextAppearance.Holo">
<!-- Alternatively, reference this font with the name "aspergit" -->
<!-- Note that only our own TextView's will use the font attribute -->
<item name="flFont">someFont</item>
<item name="android:textStyle">bold|italic</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
现在你可以使layout.xml看起来像这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.innovattic.font.FontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This uses my font in bold italic style" />
<com.innovattic.font.FontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:flFont="anotherFont"
android:textStyle="normal"
android:text="This uses another font in normal style" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
不要忘记在Android清单中应用主题.
您可以获得的所有系统默认字体位于:
android/res/values/styles.xml
您只能通过系统应用程序(例如“设置”)更改它们。也许你可以调用一些系统函数来完成它。但这似乎不太可能,因为这会给所有其他应用程序带来问题。
| 归档时间: |
|
| 查看次数: |
5682 次 |
| 最近记录: |