字体自定义字体不起作用
问题描述:
字体始终显示默认字体。字体自定义字体不起作用
我的字体都存储在资产/字体。我试图使用其他字体,并重新编码字体。 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" />
答
如果您尚未使用的字体试过前面。我会建议删除您的资产文件夹,在res文件夹中创建一个新的资产文件夹,然后将其移回原来的位置。有时Android Studio不接受构建的资产文件夹。
如下所示Runtime Exception: Font not found for every font i've tried - Android
还,我会建议创建一个扩展Button
,以便它会自动将您为小部件的字体,如果您分配正确的XML到按钮的类。
一个例子是:
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方法直接设置字样(或大多数其他窗口小部件,相同的逻辑可以应用)
答
相反,请尝试下面的代码片段:
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;
}
我知道是否面临任何问题。
答
一个不错的选择是仔细检查文件夹,字体和字体目录名拼写。一个好主意是显示任何错误,这些错误将被打印并发布给我们,以便更好地理解问题。如果您还可以发布字体名称并仔细检查它们是否相同,并确保您的资产位于正确的位置,希望我能帮到您。
好的,资产** _文件夹_ **没有问题。 我认为你提出的第二种方法很棒,但我不明白如何定义_CustomFontButton_和_CustomFontButton_customFont_。 –
在您的res文件夹中,右键单击您的值文件夹并添加一个新的xml,将其称为attrs.xml并复制我在答案末尾添加的代码 –
不,我实现了它,但没有结果。 –