2012-12-21 162 views
5

是否可以使用某種方式Typeface字體定義從窗口小部件中的EditText加載?在窗口小部件中使用帶有EditText的字體

+1

你有沒有爲此感到厭倦? –

+0

我把它轉換成了位圖,但這很痛苦。我試圖在小部件的佈局中設置自定義的'EditText',但沒有運氣(即使簡單的'CustomEditText擴展EditText'沒有內容)。 – hsz

回答

4

要使用需要駐留在資產/ fonts目錄,並且您可以訪問它像這樣的字體:

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

正如@Andro Selva在另一個答案中提出的問題 - 它是否適用於小部件? – hsz

+0

是的,它適用於我.. –

-1

您可以使用此

editText.setTypeface(Typeface.SERIF); 
+3

正確地閱讀問題。他需要資產的自定義字體 –

+0

TypeFace提供字體,不是嗎? –

+2

ya。但他需要編輯文字的自定義字體 –

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

假設你有文件的這種結構:

/assets/fonts/myfont.ttf

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

+0

這是否適用於小工具? –

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

現在我已經嘗試過這一點。它爲我工作。祝你好運

+0

你用它與小部件?我的意思是'AppWidgetProvider' – hsz

+0

不,但你有沒有試過這樣的。如果是的話,它不適合你? –

0

其他betther形式來實現這一點,並避免將字體添加到所有textview擴展TextView(或EditText或...)並將字體應用於setTypeface方法。使用這種方法,您可以控制粗體,斜體和其他樣式。

以下是擴展TextView並應用Roboto字體的類的代碼。此外,它控制了Android 4.0與HTML代碼設置Spannable時從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()); 
    } 
} 
} 
相關問題