所有textviews改变字体 [英] change font of all textviews

查看:120
本文介绍了所有textviews改变字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法改变这一切textviews的字体的布局?

is there a way to change the fonts of all textviews in a layout?

目前即时通讯使用该手动更改字体。

currently im using this to change fonts manually.

TextView txtAppName = (TextView) findViewById(R.id.txtAppName);

Typeface tf = Typeface.createFromAsset(getAssets(),
    "fonts/Existence-Light.otf");
txtAppName.setTypeface(tf);

编辑:我已编辑澄清我的问题的问题

推荐答案

也许有点晚了你,但对于其他Android用户,这可能是非常有用的。

Perhaps a little late for you, but for other Android users this may be very useful.

不幸的是,Android不提供你正在寻找的快速,简单和干净的方式来改变字体为您的整个应用程序。但最近我已经研究过这个问题,并创造了一些工具,使您可以更改字体无需任何编码(您可以通过XML,样式,甚至文本露面做这一切)。你可以阅读所有关于它这个博客,你可以下载code <一个HREF =htt​​ps://github.com/Innovattic/Android-Font-Integration>这里。

Unfortunately, Android doesn't provide the quick, easy and clean way you're looking for to change the font for your entire app. But recently I've looked into this matter and created some tools that allow you to change the font without any coding (you can do it all through xml, styles and even text appearances). You can read all about it on this blog, and you can download the code here.

下面是一个如何应用这些工具的一个例子。放入所有的字体文件资源/字体/ 。然后,声明这些字体在一个XML文件,并在你的应用程序的早期加载该文件, TypefaceManager.initialize(这一点,R.xml.fonts); (例如,在OnCreate您的应用程序类的)。 XML文件看起来是这样的:

Here's an example of how to apply these tools. Put all your font files in assets/fonts/. Then, declare those fonts in an xml file and load this file early in your app with TypefaceManager.initialize(this, R.xml.fonts); (e.g., in the onCreate of your Application class). The xml file looks like this:

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

现在你可以用它们在你的风格或XML。以下是如何的字体应用到所有文本在整个应用程序:

Now you can use them in your style or xml. Here's how to apply the font to all texts in your entire app:

<?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="font">someFont</item>
        <item name="android:textStyle">bold|italic</item>
    </style>

</resources>

现在你可以让一个layout.xml看起来是这样的:

Now you can make an layout.xml looks like this:

<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.view.FontTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This uses my font in bold italic style" />

    <com.innovattic.view.FontTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:font="anotherFont"
        android:textStyle="normal"
        android:text="This uses another font in normal style" />

</LinearLayout>

不要忘记申请的主题在你的Andr​​oid清单。

Don't forget to apply the theme in your Android manifest.

这篇关于所有textviews改变字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆