Android 中的自定义字体
Custom font in Android
所以我想根据 this 回答为我的应用程序中的按钮和文本视图制作自定义字体。我知道用这种更简单的方法是可行的,如果我只是把它放在 activity onCreate:
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 classes,就像在链接的答案中一样。我有相同的 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>
这是我的视图声明 activity xml:
<uzoltan.readout.Font.MyButton //my package name is uzoltan.readout and I have the MyButton, FontCache and CustomFontHelper classes in a package called Font
style="@style/Button"
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/login_button_text"/>
当然我有 ttf 文件,因为它与我在开始时复制的更简单的解决方案配合得很好。我观察了调试和 FontCache 中发生的事情 class 这就是发生的事情:
Typeface tf = fontCache.get(name); //returns null
if(tf == null) {
try {
tf = Typeface.createFromAsset(context.getAssets(), name); //still returns null
}
catch (Exception e) { //Runtime exception, font asset not found fonts/Gemetos.TTF
return null;
}
意味着在 CustomFontHelper class 中永远不会调用 setTypeFace。所以我的问题是 FontCache 中的 createFromAsset 调用有什么问题?为什么它 return 为空?
编辑: 我也尝试跳过 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(大写字母),所以如果我在 styles.xml 中使用 <item name="uzoltan.readout:font">fonts/Geometos.ttf</item>
则一切正常。
您可以使用自定义按钮 class 给定 below.Put 您在 asset/font 文件夹中的字体。
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" />
您可以使用书法库。
https://github.com/chrisjenx/Calligraphy
允许在 xml 中声明您的字体并快速配置它。
所以我想根据 this 回答为我的应用程序中的按钮和文本视图制作自定义字体。我知道用这种更简单的方法是可行的,如果我只是把它放在 activity onCreate:
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 classes,就像在链接的答案中一样。我有相同的 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>
这是我的视图声明 activity xml:
<uzoltan.readout.Font.MyButton //my package name is uzoltan.readout and I have the MyButton, FontCache and CustomFontHelper classes in a package called Font
style="@style/Button"
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/login_button_text"/>
当然我有 ttf 文件,因为它与我在开始时复制的更简单的解决方案配合得很好。我观察了调试和 FontCache 中发生的事情 class 这就是发生的事情:
Typeface tf = fontCache.get(name); //returns null
if(tf == null) {
try {
tf = Typeface.createFromAsset(context.getAssets(), name); //still returns null
}
catch (Exception e) { //Runtime exception, font asset not found fonts/Gemetos.TTF
return null;
}
意味着在 CustomFontHelper class 中永远不会调用 setTypeFace。所以我的问题是 FontCache 中的 createFromAsset 调用有什么问题?为什么它 return 为空?
编辑: 我也尝试跳过 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(大写字母),所以如果我在 styles.xml 中使用 <item name="uzoltan.readout:font">fonts/Geometos.ttf</item>
则一切正常。
您可以使用自定义按钮 class 给定 below.Put 您在 asset/font 文件夹中的字体。
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" />
您可以使用书法库。
https://github.com/chrisjenx/Calligraphy
允许在 xml 中声明您的字体并快速配置它。