2016-12-24 3 views
1

서체는 항상 기본 글꼴을 표시합니다.서체 사용자 정의 글꼴이 작동하지 않습니다.

내 글꼴은 자산/글꼴에 저장됩니다. 그리고 다른 글꼴을 사용하고 글꼴을 다시 인코딩하려고했습니다. 또한 PixlUI 라이브러리는 문제를 해결하지 못했습니다.

MainActivity.java

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_main); 

    Button button = (Button) findViewById(R.id.button); 
    Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/OldEnglishFive.ttf"); 
    button.setTypeface(typeface); 
} 

activity_main.xml

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="220dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:onClick="doSomething" 
    android:text="TEXT" /> 

답변

0

그렇지 이미 이전에 글꼴을 사용하여 시도합니다. 자산 폴더를 삭제하고 res 폴더 내에 새 자산 폴더를 만든 다음 이전 폴더로 이동하십시오. 때때로 Android Studio는 빌드되는 애셋 폴더를 허용하지 않습니다.

Runtime Exception: Font not found for every font i've tried - Android

또한 다음과 같이, 당신이 버튼에 대한 권리 XML을 할당 할 경우 자동으로 해당 위젯의 글꼴을 설정합니다 너무 Button를 확장하는 클래스를 생성 권하고있다.

예제는 다음과 같습니다

public class CustomFontButton extends Button { 
    AttributeSet attr; 
    public CustomFontButton(Context context) { 
     super(context); 
     setCustomFont(context, attr); 
    } 

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

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

    private void setCustomFont(Context ctx, AttributeSet attrs) { 
     String customFont = null; 
     TypedArray a = null; 
     if (attrs != null) { 
      a = ctx.obtainStyledAttributes(attrs, R.styleable.CustomFontButton); 
      customFont = a.getString(R.styleable.CustomFontButton_customFont); 
     } 
     if (customFont == null) customFont = "fonts/OldEnglishFive.ttf"; 
     setCustomFont(ctx, customFont); 
     if (a != null) { 
      a.recycle(); 
     } 
    } 

    public boolean setCustomFont(Context ctx, String asset) { 
     Typeface tf = null; 
     try { 
      tf = Typeface.createFromAsset(ctx.getAssets(), asset); 
     } catch (Exception e) { 
      Log.e("textView", "Could not get typeface", e); 
      return false; 
     } 
     setTypeface(tf); 
     return true; 
    } 
} 

다음 XML에 만

<yourpackagename.CustomFontButton 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="220dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:onClick="doSomething" 
    android:text="TEXT" /> 

변경하려면 값 폴더 내부 attrs.xml이를 만들어야합니다.

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="CustomFontButton"> 
     <attr name="customFont" format="string"/> 
    </declare-styleable> 
</resources > 

이 당신이 사용자 정의 버튼에 글꼴 싶습니다 setTypeFace 매번 할 필요 절약 할 직접 setTypeface 방법을 사용하여 서체를 설정하는 대신

+0

좋아, 자산 ** _ 폴더 _ **에는 문제가 없습니다. 당신이 제안한 두 번째 방법은 훌륭하다고 생각합니다. 그러나 _CustomFontButton_ 및 _CustomFontButton_customFont_를 정의하는 방법을 이해하지 못합니다. –

+0

res 폴더 안에 오른쪽 버튼을 클릭하고 값 xml을 추가하고 attrs.xml을 호출하고 대답 끝에 추가 한 코드를 복사하십시오. –

+0

아니요, 구현했지만 아무런 결과가 없습니다. –

0

을 (또는 대부분의 다른 위젯을, 같은 논리를 적용 할 수 있습니다) 다음 코드 스 니펫을 시도하십시오.

protected void onCreate(Bundle savedInstanceState) { 
    ... //do whatever you want 

    Button button = (Button) findViewById(R.id.button); 
    Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/OldEnglishFive.ttf"); 

    button.setText(changeFontStyle("your text", typeface)); 
} 

// custom function for changing the font style of text 
public Spannable changeFontStyle(String finalString, Typeface typeface){ 

    spannable = new SpannableString(finalString); 
    spannable.setSpan(new CustomTypefaceSpan("", typeface), 0, finalString.length(), 0); 

    return spannable; 
} 

저에게 문제가 있으면 알려주세요.

0

좋은 옵션은 폴더, 글꼴 및 글꼴 디렉토리 이름 철자를 다시 확인하는 것입니다. 인쇄 된 오류을 표시하고 문제를 조금 더 잘 이해할 수 있도록 게시하는 것이 좋습니다. 글꼴 이름을 게시하고 글꼴 이름이 같은지 다시 한 번 확인하고 자산이 올바른 위치에 있는지 확인하여 도움을받을 수 있는지 확인하십시오.

관련 문제