【问题标题】:Problem in how to integrate font for android Application如何为android应用程序集成字体的问题
【发布时间】:2011-08-29 13:23:20
【问题描述】:

我想为我的应用程序设置字体。字体就像“JOURNAL”。 但问题是,我不知道如何将它集成到我的应用程序中。如果我集成它,那么它将适用于所有应用程序还是仅适用于选定的应用程序?因为我希望它只为一个应用程序设置。不适合所有人。 那么为此我应该怎么做? 我见过here。但我认为它仅适用于 TextView 而不是整个应用程序字体。 那么,我在清单文件中有什么需要做的吗???或者我还需要做什么?? 请帮我解决这个问题。

【问题讨论】:

    标签: android android-layout fonts android-emulator android-widget


    【解决方案1】:

    我同意@Kheldar 的声明。没有任何方法可以更改 Android 应用程序中的字体。尝试使用此代码以避免每次要更改元素的字体时调用 set 方法。

    public class MyTextView extends TextView {
    
    Context context;
    String ttfName;
    
    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            this.ttfName = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.package.my", "ttf_name");
    
            init();
        }
    }
    
    private void init() {
        Typeface font = Typeface.createFromAsset(context.getAssets(), ttfName);
        setTypeface(font);
    }
    
    @Override
    public void setTypeface(Typeface tf) {
        super.setTypeface(tf);
    }
    

    }

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:package="http://schemas.android.com/apk/res/com.package.my"
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ImageView
            android:id="@+id/icon"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp" />
        <com.package.my.MyTextView
            android:id="@+id/label"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_toRightOf="@+id/icon"
            package:ttf_name="My-font.otf" />
    </RelativeLayout>
    

    【讨论】:

    • 感谢您的回复。但我有那个字体文件到我的电脑。命名为“JOURNAL.ttf”我应该把那个字体文件放在哪里??
    • 放入assets文件夹。用小写命名并更改 xml 文件以匹配文件名。
    • 假设我在布局中有按钮并且在按钮文本上我想设置这个字体呢??请帮助我解决这个问题。
    • 只需将模型的扩展名从 TextView 更改为 Button。
    • 好的。感谢它的作品很棒。我也从这里找到了一个例子。 techdroid.kbeanie.com/2011/04/…
    【解决方案2】:

    您不能从应用程序修改系统范围的字体(至少,据我所知,除非有一些利用)。

    研究这个链接:http://mobile.tutsplus.com/tutorials/android/customize-android-fonts/

    【讨论】:

    • 是的,谢谢。它真的是一个很好的教程,也可以帮助其他人。
    • 我需要的帮助很少。如果我想在 xml 文件中设置字体呢?因为在教程中,演示是为了在 java 文件中而不是在 xml 文件中设置字体。
    • 如果每次我不想用那两行来设置字体我该怎么办???
    • 你应该研究一下主题。主题正是为此目的!
    猜你喜欢
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 2012-09-05
    • 2013-11-09
    • 2011-03-23
    • 1970-01-01
    相关资源
    最近更新 更多