在窗口小部件中使用带有EditText的字体

时间:2012-12-21 11:47:15

标签: android android-widget typeface

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

6 个答案:

答案 0 :(得分:4)

您要使用的字体需要驻留在assets / fonts目录中,您可以这样访问它:

Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
edittext.setTypeface(myFont);

答案 1 :(得分:0)

editText.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/myfont.ttf"));

假设你有这种文件结构:

/assets/fonts/myfont.ttf

答案 2 :(得分: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 Development – Customize Android Fonts

答案 3 :(得分:0)

 Typeface tf   =    Typeface.createFromAsset(getAssets(),"fonts/Comic.ttf");
 youredittext.setTypeface(tf);

我现在试过这个。它对我有用。祝你好运

答案 4 :(得分:0)

其他形式实现这一点并避免将字体添加到所有textview中是扩展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());
    }
}
}

答案 5 :(得分:-1)

您可以使用此

editText.setTypeface(Typeface.SERIF);