Android - 如何为整个应用程序设置自定义字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12281761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - How to set a custom font for whole app
提问by Andrea Mario Lufino
Possible Duplicate:
Is it possible to set font for entire Application?
可能的重复:
是否可以为整个应用程序设置字体?
I need to set a custom font (.ttf format) for whole app, how can i do it? From Manifest or XML would be the best option if it is possible
我需要为整个应用程序设置自定义字体(.ttf 格式),我该怎么做?如果可能,来自 Manifest 或 XML 将是最佳选择
采纳答案by VicVu
EDIT Sept 2014:
2014 年 9 月编辑:
For anyone still seeing this old terrible answer, the real, good answer is here:
对于仍然看到这个可怕的旧答案的人来说,真正的好答案在这里:
https://stackoverflow.com/a/16883281/1154026
https://stackoverflow.com/a/16883281/1154026
OLD:
老的:
Your best bet would be this:
你最好的选择是这样的:
So
所以
TextView text = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf");
text.setTypeface(font);
With your .ttf in the root of the "assets" folder.
将 .ttf 放在“assets”文件夹的根目录中。
回答by Jelle
You could do it like this. Create a custom textview and use that one everywhere
你可以这样做。创建一个自定义的 textview 并在任何地方使用它
public class MyTextView extends android.widget.TextView
{
public MyTextView(Context context)
{
this(context, null);
}
public MyTextView(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public MyTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs);
if (this.isInEditMode()) return ;
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SomeStyle);
final String customFont = a.getString(R.styleable.SomeStyle_font);
//Build a custom typeface-cache here!
this.setTypeface(
Typeface.createFromAsset(context.getAssets(), customFont)
);
}
}
Add this to attrs.xml
将此添加到 attrs.xml
<declare-styleable name="SomeStyle">
<attr name="font" format="string" />
</declare-styleable>
Then in your theme, do this: This will make sure all the textviews will use the style MyTextView
然后在您的主题中,执行以下操作:这将确保所有文本视图都将使用样式 MyTextView
<item name="android:textViewStyle">@style/MyTextView</item>
And now we can define your custom font using your custom attribute in this style.
现在我们可以使用这种样式的自定义属性来定义您的自定义字体。
<style name="MyTextView" parent="@android:style/Widget.TextView">
<item name="font">MyPathToFontInAssets.ttf</item>
</style>
So whenever you use MyTextView in the project, it will have your custom font.
所以每当你在项目中使用 MyTextView 时,它都会有你的自定义字体。
IMPORTANT:Typefaces are not cached, so if you plan on using this code, you should also build a custom typeface-cache so you can re-use the custom typeface for all textviews. This will significantly speed-up the application!
重要提示:字体未缓存,因此如果您计划使用此代码,您还应该构建自定义字体缓存,以便您可以对所有文本视图重复使用自定义字体。这将显着加快应用程序!
UPDATE:As Amir was saying, this is almost the same as Custom fonts and XML layouts (Android)but i also use android styling to automatically use it on all textviews in the app.
更新:正如 Amir 所说,这与自定义字体和 XML 布局 (Android)几乎相同,但我也使用 android 样式在应用程序中的所有文本视图上自动使用它。
回答by Georgy Gobozov
Cerate a TextView subclass and use it everywhere
创建一个 TextView 子类并在任何地方使用它
public class RobotoTextView extends TextView {
public RobotoTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public RobotoTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RobotoTextView(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
setTypeface(tf);
}
}
}
usage
用法
<com.test.RobotoTextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
in java code you can cast it to TextView
在 Java 代码中,您可以将其转换为 TextView
回答by Aditya Nikhade
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),
"fonts/Verdana.ttf");
tv.setTypeface(face);
put the font file in the fonts folder in /res/
将字体文件放在 /res/ 的 fonts 文件夹中
Like my answer if it is helpful...
喜欢我的回答,如果有帮助的话...