自定义字体和XML布局(Android)

DrD*_*ost 174 java user-interface fonts android interface

我正在尝试使用Android中的XML文件定义GUI布局.据我所知,没有办法指定您的小部件应该在XML文件中使用自定义字体(例如,您放置在assets/font /中的字体),并且您只能使用系统安装的字体.

我知道,在Java代码中,我可以使用唯一ID手动更改每个窗口小部件的字体.或者,我可以迭代Java中的所有小部件来进行此更改,但这可能会非常慢.

我还有其他选择吗?有没有更好的方法来制作具有自定义外观的小部件?我不是特别想要为我添加的每个新小部件手动更改字体.

pet*_*ter 220

您可以扩展TextView以设置我在此处学习的自定义字体.

TextViewPlus.java:

package com.example;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

public class TextViewPlus extends TextView {
    private static final String TAG = "TextView";

    public TextViewPlus(Context context) {
        super(context);
    }

    public TextViewPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }

    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
        String customFont = a.getString(R.styleable.TextViewPlus_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }

    public boolean setCustomFont(Context ctx, String asset) {
        Typeface tf = null;
        try {
        tf = Typeface.createFromAsset(ctx.getAssets(), asset);  
        } catch (Exception e) {
            Log.e(TAG, "Could not get typeface: "+e.getMessage());
            return false;
        }

        setTypeface(tf);  
        return true;
    }

}
Run Code Online (Sandbox Code Playgroud)

attrs.xml :(在res/values中)

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

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:foo="http://schemas.android.com/apk/res/com.example"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.example.TextViewPlus
        android:id="@+id/textViewPlus1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:text="@string/showingOffTheNewTypeface"
        foo:customFont="saxmono.ttf">
    </com.example.TextViewPlus>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

你会把"saxmono.ttf"放在assets文件夹中.

更新8/1/13

这种方法存在严重的记忆问题.请参阅下面的chedabob评论.

  • 需要注意的一点是,这将产生数十个和几十个TypeFace对象并占用内存.Android 4.0之前的版本中存在一个错误,它无法正常释放TypeFaces.最简单的方法是使用HashMap创建一个TypeFace缓存.这使我的应用程序中的内存使用量从120多万桶减少到18毫升.http://code.google.com/p/android/issues/detail?id=9904 (77认同)
  • 非常重要 - 我想加倍强调chedabob的建议.除非你遵循它,否则你会在ICS之前发生内存泄漏.解决方案很少,其中一个是由chedabob提供的链接,另一个是在这里:http://stackoverflow.com/questions/8057010/listview-memory-leak.彼得 - 请更新你的答案 - 它很棒但不完整 (11认同)
  • @Majjoodi:如果您忘记将第二个命名空间添加到布局中,通常会发生这种情况:`xmlns:foo ="http:// schemas.android.com/apk/res/com.example` (7认同)
  • 这看起来不错,但是,当我尝试在main.xml中使用"TextViewPlus"时,我收到了一个错误.我得到以下内容: - 错误:解析XML时出错:未绑定前缀 - 未绑定与元素类型"supportLibs.TextViewPlus"关联的属性"foo:customFont"的前缀"foo". (5认同)

Rag*_*har 35

我参加晚会已经晚了3年:(但是对于那些可能偶然发现这篇文章的人来说,这可能会有用.

我编写了一个缓存Typefaces的库,并允许您直接从XML指定自定义字体.你可以在这里找到图书馆.

这是您使用XML布局时的样子.

<com.mobsandgeeks.ui.TypefaceTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    geekui:customTypeface="fonts/custom_font.ttf" />
Run Code Online (Sandbox Code Playgroud)

  • 从父标记中指定的命名空间 - "xmlns:geekui ="http://schemas.android.com/apk/res-auto"` (3认同)
  • 你的geekui标签的剂量来自哪里? (2认同)

Leo*_*oso 18

这可能有点晚,但您需要创建一个返回自定义字体的单例类,以避免内存泄漏.

TypeFace类:

public class OpenSans {

private static OpenSans instance;
private static Typeface typeface;

public static OpenSans getInstance(Context context) {
    synchronized (OpenSans.class) {
        if (instance == null) {
            instance = new OpenSans();
            typeface = Typeface.createFromAsset(context.getResources().getAssets(), "open_sans.ttf");
        }
        return instance;
    }
}

public Typeface getTypeFace() {
    return typeface;
}
}
Run Code Online (Sandbox Code Playgroud)

自定义TextView:

public class NativelyCustomTextView extends TextView {

    public NativelyCustomTextView(Context context) {
        super(context);
        setTypeface(OpenSans.getInstance(context).getTypeFace());
    }

    public NativelyCustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setTypeface(OpenSans.getInstance(context).getTypeFace());
    }

    public NativelyCustomTextView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        setTypeface(OpenSans.getInstance(context).getTypeFace());
    }

}
Run Code Online (Sandbox Code Playgroud)

通过xml:

<com.yourpackage.views.NativelyCustomTextView
            android:id="@+id/natively_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_margin="20dp"
            android:text="@string/natively"
            android:textSize="30sp" /> 
Run Code Online (Sandbox Code Playgroud)

编程方式:

TextView programmaticallyTextView = (TextView) 
       findViewById(R.id.programmatically_text_view);

programmaticallyTextView.setTypeface(OpenSans.getInstance(this)
                .getTypeFace());
Run Code Online (Sandbox Code Playgroud)

  • getTypeFace在每次调用时都会创建一个新的字体...这会破坏单例的目的.它应该有一个静态字段,该字段在第一次进行调用时设置,并在后续调用时返回字段的值. (2认同)

tho*_*ers 13

老问题,但在我开始自己寻找一个好的解决方案之前,我当然希望我在这里读到这个答案.书法扩展了android:fontFamily属性以在资产文件夹中添加对自定义字体的支持,如下所示:

<TextView 
  android:text="@string/hello_world"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:fontFamily="fonts/Roboto-Bold.ttf"/>
Run Code Online (Sandbox Code Playgroud)

要激活它,您唯一需要做的就是将它附加到您正在使用的Activity的Context中:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(new CalligraphyContextWrapper(newBase));
}
Run Code Online (Sandbox Code Playgroud)

您还可以指定要替换的自定义属性 android:fontFamily

它也适用于主题,包括AppTheme.


Chu*_*ulo 8

使用DataBinding:

@BindingAdapter({"bind:font"})
public static void setFont(TextView textView, String fontName){
 textView.setTypeface(Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/" + fontName));
}
Run Code Online (Sandbox Code Playgroud)

在XML中:

<TextView
app:font="@{`Source-Sans-Pro-Regular.ttf`}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

字体文件必须在 assets/fonts/


Tor*_*erg 7

如果你只有一个字体,你想补充,并想少写代码,您可以为您的特定字体专用的TextView.见下面的代码.

package com.yourpackage;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class FontTextView extends TextView {
    public static Typeface FONT_NAME;


    public FontTextView(Context context) {
        super(context);
        if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/FontName.otf");
        this.setTypeface(FONT_NAME);
    }
    public FontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/FontName.otf");
        this.setTypeface(FONT_NAME);
    }
    public FontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/FontName.otf");
        this.setTypeface(FONT_NAME);
    }
}
Run Code Online (Sandbox Code Playgroud)

在main.xml中,您现在可以像这样添加textView:

<com.yourpackage.FontTextView
    android:id="@+id/tvTimer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
Run Code Online (Sandbox Code Playgroud)


小智 7

最好的方法从Android O预览版本就是这样
1.)右键单击res文件夹,然后转到新建> Android资源目录.将
出现"新建资源目录"窗口.
2.)在"资源类型"列表中,选择" 字体",然后单击"确定".
3.)在字体文件夹中添加字体文件.下面的文件夹结构生成R.font.dancing_script,R.font.la_la和R.font.ba_ba.
4.)双击字体文件以在编辑器中预览文件的字体.

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

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

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
    android:fontStyle="normal"
    android:fontWeight="400"
    android:font="@font/hey_regular" />
    <font
    android:fontStyle="italic"
    android:fontWeight="400"
    android:font="@font/hey_bababa" />
</font-family>
Run Code Online (Sandbox Code Playgroud)

将字体添加到TextView:

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

从文档中可以看出

使用字体

所有步骤都是正确的.


Nat*_*ann 5

扩展TextView并为其提供自定义属性,或者只使用android:tag属性传入要使用的字体的String.您将需要选择一个约定并坚持使用它,例如我将所有字体放在res/assets/fonts /文件夹中,以便TextView类知道在哪里找到它们.然后在构造函数中,您只需在超级调用后手动设置字体.


Tar*_*Sha 4

使用自定义字体的唯一方法是通过源代码。

请记住,Android 运行在资源非常有限的设备上,并且字体可能需要大量 RAM。内置的 Droid 字体是专门制作的,如果您注意到的话,会缺少许多字符和装饰。

  • 我会做更多的事情来考虑 tareqHs 答案的背景,该答案比你的评论早了两年半。 (9认同)