그렇지 이미 이전에 글꼴을 사용하여 시도합니다. 자산 폴더를 삭제하고 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 방법을 사용하여 서체를 설정하는 대신
좋아, 자산 ** _ 폴더 _ **에는 문제가 없습니다. 당신이 제안한 두 번째 방법은 훌륭하다고 생각합니다. 그러나 _CustomFontButton_ 및 _CustomFontButton_customFont_를 정의하는 방법을 이해하지 못합니다. –
res 폴더 안에 오른쪽 버튼을 클릭하고 값 xml을 추가하고 attrs.xml을 호출하고 대답 끝에 추가 한 코드를 복사하십시오. –
아니요, 구현했지만 아무런 결과가 없습니다. –