Android - 使用自定义字体

RAT*_*AKE 262 android custom-font android-fonts android-typeface

我将自定义字体应用于a TextView,但似乎没有更改字体.

这是我的代码:

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
    TextView myTextView = (TextView)findViewById(R.id.myTextView);
    myTextView.setTypeface(myTypeface);
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我解决这个问题吗?

Oct*_*ean 227

在Mobiletuts +上有关于Android文本格式的非常好的教程.快速提示:自定义Android字体

编辑:现在自己测试.这是解决方案.您可以使用名为fonts的子文件夹,但它必须放在assets文件夹而不是res文件夹中.所以

资产/字体

还要确保字体结尾我的意思是字体文件本身的结尾都是小写的.换句话说,它不应该是,myFont.TTFmyfont.ttf 这种方式必须是小写的

  • 如果您正在学习该教程,请确保使用路径Typeface.createFromAsset(getAssets(),"fonts/yourfont.ttf"); 如果你把它放在fonts子目录中 (5认同)
  • 当然这是拉链项目的链接.http://dl.dropbox.com/u/8288893/customFont.zip (2认同)

jav*_*ian 55

在尝试了这个帖子中描述的大部分解决方案之后,我偶然发现了书法(https://github.com/chrisjenx/Calligraphy) - 一个由Christopher Jenkins创建的库,可以让你轻松地为你的应用添加自定义字体.与此处提出的方法相比,他的lib的优点是:

  1. 您不必介绍自己的覆盖TextView组件,而是使用内置的TextView
  2. 您可以使用gradle轻松地包含库
  3. 该库不限制您选择的字体; 你只需将你喜欢的那些添加到资产目录
  4. 您不仅可以获得自定义文本视图 - 还将使用您的自定义字体显示所有其他基于文本的Android组件.


esp*_*chi 32

我知道已有很好的答案,但这是一个完全有效的实现.

这是自定义文本视图:

package com.mycompany.myapp.widget;

/**
 * Text view with a custom font.
 * <p/>
 * In the XML, use something like {@code customAttrs:customFont="roboto-thin"}. The list of fonts
 * that are currently supported are defined in the enum {@link CustomFont}. Remember to also add
 * {@code xmlns:customAttrs="http://schemas.android.com/apk/res-auto"} in the header.
 */
public class CustomFontTextView extends TextView {

    private static final String sScheme =
            "http://schemas.android.com/apk/res-auto";
    private static final String sAttribute = "customFont";

    static enum CustomFont {
        ROBOTO_THIN("fonts/Roboto-Thin.ttf"),
        ROBOTO_LIGHT("fonts/Roboto-Light.ttf");

        private final String fileName;

        CustomFont(String fileName) {
            this.fileName = fileName;
        }

        static CustomFont fromString(String fontName) {
            return CustomFont.valueOf(fontName.toUpperCase(Locale.US));
        }

        public Typeface asTypeface(Context context) {
            return Typeface.createFromAsset(context.getAssets(), fileName);
        }
    }

    public CustomFontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        if (isInEditMode()) {
            return;
        } else {
            final String fontName = attrs.getAttributeValue(sScheme, sAttribute);

            if (fontName == null) {
                throw new IllegalArgumentException("You must provide \"" + sAttribute + "\" for your text view");
            } else {
                final Typeface customTypeface = CustomFont.fromString(fontName).asTypeface(context);
                setTypeface(customTypeface);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是自定义属性.这应该转到你的res/attrs.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomFontTextView">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

这就是你如何使用它.我将使用相对布局来包装它并显示customAttr声明,但它显然可能是您已有的任何布局.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:customAttrs="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.mycompany.myapp.widget.CustomFontTextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="foobar"
         customAttrs:customFont="roboto_thin" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)


ben*_*nvd 14

我以前成功使用过它.我们的实现之间的唯一区别是我没有在资产中使用子文件夹.但不确定这是否会改变任何事情.


Jel*_*sen 14

如果您将字体放在正确的位置并且字体文件本身没有错误,那么您的代码应该像RATTLESNAKE一样工作.

但是,如果你可以在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" >

    <!-- This text view is styled with the app theme -->
    <com.innovattic.font.FontTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This uses my font in bold italic style" />

    <!-- This text view is styled here and overrides the app theme -->
    <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" />

    <!-- This text view is styled with a style and overrides the app theme -->
    <com.innovattic.font.FontTextView
        style="@style/StylishFont"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This also uses another font in normal style" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

随附res/values/styles.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>

    <!-- Alternative style, maybe for some other widget -->
    <style name="StylishFont">
        <item name="flFont">anotherFont</item>
        <item name="android:textStyle">normal</item>
    </style>

</resources>
Run Code Online (Sandbox Code Playgroud)

我专门为此创建了一些工具.请参阅GitHub的这个项目,或者看一下这个解释整个事情的博客文章.


Faa*_*hir 12

对于android中的自定义字体,在资源文件夹名称中创建一个文件夹,它"fonts"将所需的fonts.ttf或.otf文件放入其中.

如果您扩展UIBaseFragment:

Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Arial.ttf");
        tv.setTypeface(font);
Run Code Online (Sandbox Code Playgroud)

否则如果扩展活动:

Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/Arial.ttf");
        tv.setTypeface(font);
Run Code Online (Sandbox Code Playgroud)


小智 12

最好的方法从Android O预览版开始就是这样:

只有你有android studio-2.4或更高版本才有效

  1. 右键单击res文件夹,然后转到" 新建">"Android资源目录".将
    出现"新建资源目录"窗口.
  2. 在"资源类型"列表中,选择" 字体",然后单击"确定".
  3. 添加的字体文件夹的字体文件下面.The文件夹结构生成R.font.dancing_script,R.font.la_laR.font.ba_ba.
  4. 双击字体文件以在编辑器中预览文件的字体.

接下来我们必须创建一个字体系列:

  1. 右键单击字体文件夹,然后转到" 新建">"字体资源文件".将出现"新建资源文件"窗口.
  2. 输入文件名,然后单击"确定".新的字体资源XML在编辑器中打开.
  3. 将每个字体文件,样式和权重属性括在font标签元素中.以下XML说明了在字体资源XML中添加与字体相关的属性:

将字体添加到TextView:

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/hey_fontfamily"/>
Run Code Online (Sandbox Code Playgroud)

从文档中可以看出

使用字体

所有步骤都是正确的.

  • ony api> 26(( (4认同)

小智 7

您可以在https://github.com/neopixl/PixlUI上使用PixlUI

导入他们的.jar并在XML中使用它

 <com.neopixl.pixlui.components.textview.TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    pixlui:typeface="GearedSlab.ttf" />
Run Code Online (Sandbox Code Playgroud)