2012-12-21 7 views
5

어떻게 든 위젯 EditText과 자산에서로드 Typeface 글꼴 정의를 사용할 수 있습니까?사용 서체

+1

지루한 점이 있습니까? –

+0

나는 비트 맵 폰트를 변환하여 그것을했다,하지만 고통스러운. 나는 위젯의 레이아웃에서 커스텀'EditText'를 설정하려했지만 행운은 없습니다 (간단한'CustomEditText extends EditText'에서 내용 없음). – hsz

답변

4

당신이 자산/fonts 디렉토리에 상주 할 필요를 사용하려면, 당신은과 같이 액세스 글꼴 : 지금이 시도

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

다른 대답에 요청 -하지가 위젯 작동? – hsz

+0

네, 저에게 맞습니다. –

-1

당신이 사용할 수있는이 당신이 파일의 구조가 가정

editText.setTypeface(Typeface.SERIF); 
+3

질문을 올바르게 읽으십시오. 그는이 –

+0

서체 글꼴을 제공 자산의 사용자 지정 글꼴을하지 않습니다 필요 ?? –

+2

예. 하지만 edittext –

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

: 그에 대한 코드 아래를 참조하십시오

0

/assets/fonts/myfont.ttf을, 그것을 당신의 문제를 해결할 것입니다.

// 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

용 사용자 정의 글꼴이 필요합니다. 위젯에서이 기능을 사용할 수 있습니까? @Andro 셀바으로 –

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

. 그것은 나를 위해 일했습니다. 행운을 빌어 요

+0

위젯과 함께 사용 해본 적이 있습니까? 나는'AppWidgetProvider'을 말합니다. – hsz

+0

아니요. 그렇다면 그것은 당신에게 효과가 없습니까? –

0

다른 betther 형식은 이것을 구현하고 모든 textviews에 글꼴을 추가하는 것을 피하기 위해 TextView (또는 EditText 또는 ...)를 확장하고 setTypeface 메서드에 글꼴을 적용합니다. 이 방법으로 굵게, 기울임 꼴 및 다른 스타일을 제어 할 수 있습니다. 여기

는 텍스트 뷰를 확장하고 Roboto로 폰트를 적용한 클래스에 대한 코드이다. 또한 HTML 4.0에서 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()); 
    } 
} 
}