android-layout - 使用自定义字体时,DocumentView 显示错误

标签 android-layout android-fonts document-view

friend 我正在使用 DocumentView 来调整并在其中使用自定义字体。 我可以很好地调整字体,但是当我将自定义字体的路径放入其中时。它向我显示错误。

这是我的项目。

http://www.mediafire.com/download/ggobgpzzxjc412m/CustomFontTest.zip

这是我的错误堆栈。

Process: com.example.xperts98.customfonttest, PID: 11279
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xperts98.customfonttest/com.example.xperts98.customfonttest.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class com.bluejamesbond.text.DocumentView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
        at `enter code here`android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
        at android.app.ActivityThread.access$800(ActivityThread.java:148)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
 Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.bluejamesbond.text.DocumentView
        at android.view.LayoutInflater.createView(LayoutInflater.java:633)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
        at com.example.xperts98.customfonttest.MainActivity.onCreate(MainActivity.java:17)
        at android.app.Activity.performCreate(Activity.java:5953)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
        at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.example.xperts98.customfonttest.MainActivity.onCreate(MainActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:5953)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
 Caused by: java.lang.RuntimeException: Font asset not found fonts/ProximaNova-Light.ttf
        at android.graphics.Typeface.createFromAsset(Typeface.java:190)
        at com.bluejamesbond.text.DocumentView.initDocumentView(DocumentView.java:315)
        at com.bluejamesbond.text.DocumentView.<init>(DocumentView.java:107)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.example.xperts98.customfonttest.MainActivity.onCreate(MainActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:5953)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

最佳答案

首先,您必须使用 android studio 向导在“main”文件夹中创建“assets”文件夹。

这是说明该过程的图片。

enter image description here

其次,您必须在“assets”文件夹下创建“fonts”文件夹。 这是说明该过程的图片。 enter image description here

第三,您已将 font.ttf/font.otf 文件放入字体文件夹中。

这是activity_main.xml代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/my_text"
        android:textAppearance="?android:attr/textAppearanceLarge" />


</LinearLayout>

这是MainActivity.java代码

package com.example.documentviewwithcustomfont;

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.bluejamesbond.text.DocumentView;
import com.bluejamesbond.text.style.TextAlignment;

public class MainActivity extends AppCompatActivity {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textView);

        DocumentView documentView = new DocumentView(this, DocumentView.PLAIN_TEXT);
        documentView.getDocumentLayoutParams().setTextAlignment(TextAlignment.JUSTIFIED);

        Typeface font = Typeface.createFromAsset(getAssets(), "fonts/proxima_nova_regular.ttf");

        textView.setTypeface(font);


    }

}

下面是您可以下载整个项目的链接。

http://www.mediafire.com/download/28726mwwhvua5nd/DocumentViewWithCustomFont.rar

关于android-layout - 使用自定义字体时,DocumentView 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30950514/

相关文章:

android - 如何在Android应用程序中添加字体

c++ - 如何在保持文档打开的同时关闭 MFC CVIEW

c++ - 获取文档类名称

android - 以编程方式将 LinearLayout 高度更改为 0

android - 如何使所有复选框在gridimage的Longclick上可见

android - 差异 : android:background and android:src?

android - RippleDrawable 不在 View 上绘制

android - 如何获取 TextView 上应用的字体名称

android - chris Jenx for Android Q 的书法库崩溃

c++ - 使用 CString 的 MFC TextOut 失败