自定义字体和 XML 布局 (Android) [英] Custom fonts and XML layouts (Android)

查看:33
本文介绍了自定义字体和 XML 布局 (Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

解决方案

你可以扩展 TextView 来设置自定义字体,正如我所学到的 此处.

TextViewPlus.java:

package com.example;导入 android.content.Context;导入 android.content.res.TypedArray;导入 android.graphics.Typeface;导入 android.util.AttributeSet;导入 android.util.Log;导入 android.widget.TextView;公共类 TextViewPlus 扩展了 TextView {private static final String TAG = "TextView";公共 TextViewPlus(上下文上下文){超级(上下文);}公共 TextViewPlus(上下文上下文,AttributeSet attrs){超级(上下文,属性);setCustomFont(context, attrs);}公共 TextViewPlus(上下文上下文,AttributeSet attrs,int defStyle){超级(上下文,属性,defStyle);setCustomFont(context, attrs);}私有无效 setCustomFont(上下文 ctx,AttributeSet attrs){TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);String customFont = a.getString(R.styleable.TextViewPlus_customFont);setCustomFont(ctx, customFont);a.recycle();}公共布尔 setCustomFont(上下文 ctx,字符串资产){字体 tf = null;尝试 {tf = Typeface.createFromAsset(ctx.getAssets(), asset);} 捕获(异常 e){Log.e(TAG, "无法获取字体:"+e.getMessage());返回假;}设置字体(tf);返回真;}}

attrs.xml:(在资源/值中)

ma​​in.xml:

<com.example.TextViewPlusandroid: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>

您可以将saxmono.ttf"放在assets 文件夹中.

更新 8/1/13

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

I'm trying to define a GUI layout using XML files in Android. As far as I can find out, there is no way to specify that your widgets should use a custom font (e.g. one you've placed in assets/font/) in XML files and you can only use the system installed fonts.

I know that, in the Java code, I could change the font of each widget manually using unique IDs. Alternatively, I could iterate over all the widgets in Java to make this change, but this would probably be very slow.

What other options do I have? Is there any better ways to making widgets that have a custom look? I don't particularly want to have to manually change the font for every new widget I add.

解决方案

You can extend TextView to set custom fonts as I learned here.

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;
    }

}

attrs.xml: (in res/values)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextViewPlus">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources>

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>

You would put "saxmono.ttf" in the assets folder.

UPDATE 8/1/13

There are serious memory concerns with this method. See chedabob's comment below.

这篇关于自定义字体和 XML 布局 (Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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