5

是否可以使用从小部件中Typeface的资产加载的某种字体定义EditText

4

6 回答 6

4

您要使用的字体需要位于 assets/fonts 目录中,您可以像这样访问它:

Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
edittext.setTypeface(myFont);
于 2012-12-21T12:03:22.717 回答
0
editText.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/myfont.ttf"));

假设您具有以下文件结构:

/assets/fonts/myfont.ttf

于 2012-12-21T11:58:53.967 回答
0

请参阅下面的代码,它将解决您的问题。

// text view label
TextView mTextView1 = (TextView) findViewById(R.id.TextView1);

// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), "DroidSansFallback.ttf");

// Applying font
mTextView1.setTypeface(tf);

有关更多信息,请参见下面的链接。

Android 开发 - 自定义 Android 字体

于 2012-12-21T11:58:54.033 回答
0
 Typeface tf   =    Typeface.createFromAsset(getAssets(),"fonts/Comic.ttf");
 youredittext.setTypeface(tf);

我现在已经试过了。它对我有用。祝你好运

于 2012-12-21T12:05:22.817 回答
0

实现这一点并避免将字体添加到所有文本视图的其他更好的形式是扩展 TextView (或 EditText 或 ... )并将字体应用于 setTypeface 方法。使用此方法,您可以控制粗体、斜体和其他样式。

这是扩展 TextView 并应用 Roboto 字体的类的代码。此外,当从 HTML 设置 Spannable 时,它​​还控制了 Android 4.0 与 html 代码相关的一些错误

public class TextViewRoboto extends TextView {  
public static final String TAG = "TextViewRoboto";

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

public TextViewRoboto(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public TextViewRoboto(Context context) {
    super(context);
}

@Override
public void setTypeface(Typeface tf, int style) {

    //This is to override eclipse error messages
    if (!super.isInEditMode()) {      

        if (style == Typeface.BOLD)  
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Bold.ttf"));
        else if (style == Typeface.ITALIC)
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Italic.ttf"));
        else
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf"));    
    }    
}


//
// With this code aboid the <b> and <strong> problem on Jelly Bean
//

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    try{
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }catch (ArrayIndexOutOfBoundsException e){
        //Logger.w(TAG, "Problem onMeasure. Set normal text");
        setText(getText().toString());
        super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    }
}   
@Override
public void setGravity(int gravity){
    try{
        super.setGravity(gravity);
    }catch (ArrayIndexOutOfBoundsException e){
        //Logger.w(TAG, "Problem setGravity. Set normal text");
        setText(getText().toString());
        super.setGravity(gravity); 
    }
}
@Override
public void setText(CharSequence text, BufferType type) {
    try{
        super.setText(text, type);
    }catch (ArrayIndexOutOfBoundsException e){
        //Logger.w(TAG, "Problem on setText. Set normal text");
        setText(text.toString());
    }
}

public void setHTMLText(CharSequence text, BufferType type) {
    String tmpText = text.toString();
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        tmpText = tmpText.replace("<strong>", "<b>");
        tmpText = tmpText.replace("</strong>", "</b>");
        tmpText = tmpText.replace("<em>", "<i>");
        tmpText = tmpText.replace("</em>", "</i>");
        text = tmpText;
    }

    try{
        super.setText(Html.fromHtml(tmpText), type);
    }catch (ArrayIndexOutOfBoundsException e){
        //Logger.w(TAG, "Problem on setText. Set normal text");
        setText(text.toString());
    }
}
}
于 2012-12-21T12:23:18.513 回答
-1

你可以用这个

editText.setTypeface(Typeface.SERIF);
于 2012-12-21T11:51:21.840 回答