Android中的自定义字体
所以我想根据答案为我的应用程序中的按钮和文本视图创建一个自定义字体。我知道用这种更简单的方法是可行的,如果我把它放在活动onCreate中:Android中的自定义字体,android,android-view,android-fonts,android-typeface,Android,Android View,Android Fonts,Android Typeface,所以我想根据答案为我的应用程序中的按钮和文本视图创建一个自定义字体。我知道用这种更简单的方法是可行的,如果我把它放在活动onCreate中: String fontPath = "fonts/Geometos.ttf"; Button mButton = (Button) findViewById(R.id.login_button); Typeface tf = Typeface.createFromAsset(getAssets(), fontPath); m
String fontPath = "fonts/Geometos.ttf";
Button mButton = (Button) findViewById(R.id.login_button);
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
mButton.setTypeface(tf);
但如果我想在许多视图中使用自定义字体,这不是一个好的解决方案
我创建了CustomFontHelper、FontCache和MyButton类,就像在链接答案中一样。我有相同的attrs.xml,我的style.xml如下所示:
<style name="Button" parent="@android:style/Widget.Button">
<item name="android:background">@color/colorPrimary</item>
<item name="android:textSize">50sp</item>
<item name="android:textColor">@color/SecondaryTextColor</item>
<item name="uzoltan.readout:font">fonts/Geometos.TTF</item>
</style>
这意味着在CustomFontHelper类中永远不会调用setTypeFace。所以我的问题是FontCache中的createFromAsset调用有什么问题?为什么返回空值
编辑:我还尝试跳过FontCache,并在CustomFontHelper中使用此行:
Typeface tf = Typeface.createFromAsset(context.getAssets(), font);
但这会导致运行时错误,原因与“java.lang.RuntimeException:Font asset not found fonts/Geometos.TTF”相同
(我的字体目录是src/main/assets/fonts/Geometos.TTF
编辑2:问题是.TTF(大写),因此如果我使用字体/Geometos.TTF
在styles.xml中,一切都可以正常工作。您可以使用下面给出的自定义按钮类。将字体放在资产/字体文件夹中
public class CustomButton extends Button{
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
// TODO Auto-generated constructor stub
}
public CustomButton(Context context) {
super(context);
init();
// TODO Auto-generated constructor stub
}
public CustomButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
// TODO Auto-generated constructor stub
}
private void init(){
Typeface font_type=Typeface.createFromAsset(getContext().getAssets(), "font/ProximaNova-Bold.ttf");
setTypeface(font_type);
}
}
现在您可以使用xml中的按钮,如下所示
<model.CustomButton
android:id="@+id/search"
android:layout_width="@dimen/edittext_width_large"
android:layout_height="@dimen/button_height"
android:layout_below="@+id/cars"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/pad_20dp"
android:background="@drawable/button_pressed_bg"
android:text="@string/find_car"
android:textColor="@color/white" />
您可以使用书法库
允许用xml声明您的字体并快速配置。检查@jinosh answer我会接受您的答案,因为它告诉我font/Geometos.TTF是错误的,我应该在my styles.xml中使用font/Geometos.TTF,它起作用了:D我将对原始海报的另一个问题发表评论,也许他会编辑他的anwser,这是我出于某种原因使用TTF的唯一原因。或者不是,我还需要4个声誉,这样我才能对其他问题发表评论。书法也可以让你选择默认字体。
public class CustomButton extends Button{
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
// TODO Auto-generated constructor stub
}
public CustomButton(Context context) {
super(context);
init();
// TODO Auto-generated constructor stub
}
public CustomButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
// TODO Auto-generated constructor stub
}
private void init(){
Typeface font_type=Typeface.createFromAsset(getContext().getAssets(), "font/ProximaNova-Bold.ttf");
setTypeface(font_type);
}
}
<model.CustomButton
android:id="@+id/search"
android:layout_width="@dimen/edittext_width_large"
android:layout_height="@dimen/button_height"
android:layout_below="@+id/cars"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/pad_20dp"
android:background="@drawable/button_pressed_bg"
android:text="@string/find_car"
android:textColor="@color/white" />