【问题标题】:Android setTypeface not working correctlyAndroid setTypeface 无法正常工作
【发布时间】:2014-09-17 05:46:21
【问题描述】:

我在 android 中编写简单的新应用程序,我想将 setTypeface 用于 Activity 小部件。

在下面的代码中,这似乎是正确的,但我在控制台中收到此错误:

1803-1803/com.example.AndroidMultiPage E/AndroidRuntime﹕ 
     FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity
    ComponentInfo{com.example.AndroidMultiPage/
    com.example.AndroidMultiPage.MyActivity}: 
    java.lang.RuntimeException: 

    native typeface cannot be made

字体路径:assets/font/BZar.ttf

我的简单代码:

package com.example.AndroidMultiPage;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;

public class MyActivity extends Activity {
    private Button submit;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Typeface face = Typeface.createFromAsset(getAssets(),
                "font/BZar.ttf");

        Button   submit   = (Button)   findViewById(R.id.submitButton);

        submit.setTypeface(face);

}

【问题讨论】:

标签: android android-activity


【解决方案1】:

这是因为它是“字体”(复数)而不是“字体”。

你有'assets/font/your_font',它应该是'assets/fonts/your_font'。 重命名文件夹并更新代码。

【讨论】:

    【解决方案2】:
    public void setFont(Context context, ViewGroup vg, String fontInAssetFolder) {
        final Typeface font=Typeface.createFromAsset(context.getAssets(), fontInAssetFolder);
        for (int i = 0; i < vg.getChildCount(); i++) {
            final View v = vg.getChildAt(i);
            if (v instanceof ViewGroup)
                setFont(context, (ViewGroup) v, fontInAssetFolder);
            else if (v instanceof TextView) {
                Log.e("FONT", "IS = "+ font.toString()+ "; is " + ((TextView)v).getText());
                v.post(new Runnable() {
    
                    @Override
                    public void run() {
                        (((TextView) v)).setTypeface(font);
                    }
                });
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      尝试将字体文件保存在资产文件夹中,并尝试使用不同的字体文件。

      【讨论】:

        猜你喜欢
        • 2017-03-10
        • 2012-09-13
        • 2012-03-24
        • 2012-11-03
        • 2013-11-24
        • 1970-01-01
        • 2011-07-04
        • 2015-10-04
        • 2015-06-04
        相关资源
        最近更新 更多