2017-08-31 11 views
-6

以下のコードは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); 
     } 
    } 
} 

これは他のアプリで使用されていて、うまく機能します。

私のアプリケーションでカスタムテキストビューを使用すると、インフレーションエラーが発生します。 trebuchetフォントをassets/fontsフォルダに含めました。

+3

あなただけのアプリのすべてのTextView年代の書体を変更したい場合は、質問 –

+0

とあなたのエラーを共有し、あなたは別のアプローチを使用することができます。 – Khan

+0

xmlファイルの完全なパッケージパスでカスタムtextviewを使用します。 getContext()。getResources()。getString(R.string.normal_font_path) ' – Piyush

答えて

0

をこの行を変更カスタム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" /> 
+0

それは動作しませんでした。同じエラーが発生します。 –

+0

あなたが直面しているエラー –

+0

その理由は、フォントが間違っていたからです。ごめんなさい... –

0

この

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

のようなあなたのlayout.xmlファイルでの完全なパッケージパスを持つこのカスタムtextviewを試してみて、ため、この次のコードを試してみてください、あなたのcustomtextviewに

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

私はすでに行った.. –

+0

その理由は、フォントが間違っていたからです。申し訳ありません... –

+0

@권준동よろしいですか?アップヴォートし、受け入れられたとして回答をマークすることを忘れないでください –

関連する問題