【问题标题】:the error occurs when I use custom textview in the android studio当我在 android studio 中使用自定义 textview 时发生错误
【发布时间】:2017-08-31 05:55:28
【问题描述】:

下面的代码是使用 trebuchet 字体的自定义 textview 类。

public class TrebuchetTextView extends TextView {

    public TrebuchetTextView(Context context) {
        super(context);

        init();
    }

    public TrebuchetTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();
    }

    public TrebuchetTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        init();
    }

    private void init() {
        if (!isInEditMode()) {
            final Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), getContext().getString(R.string.normal_font_path));
        setTypeface(typeface);
        }
    }
}

这在其他应用程序中使用并且效果很好。

但是当我在我的应用程序中使用自定义文本视图时会发生膨胀错误。 我在 assets/fonts 文件夹中包含了 trebuchet 字体。

【问题讨论】:

  • 用问题分享你的错误
  • 如果你只是想改变app的所有TextView的字体,你可以使用另一种方法。
  • 在 xml 文件中使用带有完整包路径的自定义文本视图。这也必须是getContext().getResources().getString(R.string.normal_font_path)
  • the inflation error occurs 那么你应该修复它。但是由于您没有说出哪个错误,所以没有显示它的堆栈跟踪 - 祝您自己修复它好运。

标签: java android


【解决方案1】:

尝试使用以下代码自定义textview

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class FontTextView extends TextView {


    public FontTextView(Context context) {
      super(context);
      Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf"); 
      this.setTypeface(face); 
    }

    public FontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
     Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf"); 
  this.setTypeface(face); 
    }

    public FontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
     Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf"); 
  this.setTypeface(face); 
    }

    protected void onDraw (Canvas canvas) {
        super.onDraw(canvas);


    }

}

在xml中:

<com.util.FontTextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/accountInfoText"
                    android:textColor="#727272"
                    android:textSize="18dp" />

【讨论】:

    【解决方案2】:

    试试这个你的自定义textview,它的完整包路径在你的 layout.xml 文件中,像这样

    <com.example.pkg.TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    

    并在您的 customtextview 中更改此行

    Typeface face=Typeface.createFromAsset(context.getAssets(), "yourfont.ttf"); 
    

    【讨论】:

      猜你喜欢
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多