gpt4 book ai didi

java - Typeface 自定义字体不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 11:56:46 26 4
gpt4 key购买 nike

字体始终显示默认字体。

我的字体存储在 Assets /字体中。我尝试过使用其他字体,并重新编码字体。 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" />

最佳答案

如果您之前尚未尝试过使用某种字体。我建议删除您的 Assets 文件夹,在 res 文件夹中创建一个新的 Assets 文件夹,然后将其移回之前的位置。有时 Android Studio 不接受构建的 Assets 文件夹。

如图所示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(或大多数其他小部件,可以应用相同的逻辑)

关于java - Typeface 自定义字体不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41316161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com