如何改变字体在TextView,默认它显示为Arial?怎么改成Helvetica字体?
当前回答
也许可以简单一点:
public class Fonts {
public static HashSet<String,Typeface> fonts = new HashSet<>();
public static Typeface get(Context context, String file) {
if (! fonts.contains(file)) {
synchronized (this) {
Typeface typeface = Typeface.createFromAsset(context.getAssets(), name);
fonts.put(name, typeface);
}
}
return fonts.get(file);
}
}
// Usage
Typeface myFont = Fonts.get("arial.ttf");
(请注意,此代码未经测试,但通常这种方法应该工作良好。)
其他回答
以上答案是正确的。只要确保你在“assets”文件夹下创建了一个名为“fonts”的子文件夹,如果你正在使用这段代码。
当你的字体存储在res/asset/fonts/Helvetica.ttf中时,使用以下方法:
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/Helvetica.ttf");
txt.setTypeface(tf);
或者,如果你的字体文件存储在res/font/helvetica.ttf中,使用以下方法:
Typeface tf = ResourcesCompat.getFont(this,R.font.helvetica);
txt.setTypeface(tf);
首先,默认不是Arial。默认为Droid Sans。
其次,要更改为不同的内置字体,请在布局XML中使用android:typeface或在Java中使用setTypeface()。
第三,Android中没有Helvetica字体。内置选项有Droid Sans (Sans)、Droid Sans Mono (monospace)和Droid Serif (Serif)。虽然您可以将自己的字体与应用程序捆绑在一起,并通过setTypeface()使用它们,但请记住,字体文件很大,在某些情况下需要许可协议(例如Helvetica,一种Linotype字体)。
EDIT
The Android design language relies on traditional typographic tools such as scale, space, rhythm, and alignment with an underlying grid. Successful deployment of these tools is essential to help users quickly understand a screen of information. To support such use of typography, Ice Cream Sandwich introduced a new type family named Roboto, created specifically for the requirements of UI and high-resolution screens. The current TextView framework offers Roboto in thin, light, regular and bold weights, along with an italic style for each weight. The framework also offers the Roboto Condensed variant in regular and bold weights, along with an italic style for each weight.
ICS之后,android包含了Roboto字体样式, 阅读更多Roboto
编辑2
随着支持库26的出现,Android现在支持自定义字体 违约。您可以在res/fonts中插入新的字体,这些字体可以单独以XML或编程方式设置为TextViews。整个应用程序的默认字体也可以通过定义它styles.xml来改变,android开发者文档对此有明确的指导
添加类FontTextView.java:
public class FontTextView extends TextView {
String fonts[] = {"HelveticaNeue.ttf", "HelveticaNeueLight.ttf", "motschcc.ttf", "symbol.ttf"};
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if (!isInEditMode()) {
init(attrs);
}
}
public FontTextView(Context context) {
super(context);
if (!isInEditMode()) {
init(null);
}
}
private void init(AttributeSet attrs) {
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.FontTextView);
if (a.getString(R.styleable.FontTextView_font_type) != null) {
String fontName = fonts[Integer.valueOf(a.getString(R.styleable.FontTextView_font_type))];
if (fontName != null) {
Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/" + fontName);
setTypeface(myTypeface);
}
a.recycle();
}
}
}
}
添加到资产库字体
添加到attrs.xml, 数字应该在数组类中的顺序。 < declare-styleable name = " FontTextView " > <attr name="font_type" format="enum"> <enum name="HelveticaNeue" value="0"/> <enum name="HelveticaNeueLight" value="1"/> <enum name="motschcc" value="2"/> <enum name="symbol" value="3"/> < / attr >
从列表中选择字体
import java.lang.ref.WeakReference;
import java.util.HashMap;
import android.content.Context;
import android.graphics.Typeface;
public class FontsManager {
private static FontsManager instance;
private static HashMap<String, WeakReference<Typeface>> typefaces = new HashMap<String, WeakReference<Typeface>>();
private static Context context;
private FontsManager(final Context ctx) {
if (context == null) {
context = ctx;
}
}
public static FontsManager getInstance(final Context appContext) {
if (instance == null) {
instance = new FontsManager(appContext);
}
return instance;
}
public static FontsManager getInstance() {
if (instance == null) {
throw new RuntimeException(
"Call getInstance(Context context) at least once to init the singleton properly");
}
return instance;
}
public Typeface getFont(final String assetName) {
final WeakReference<Typeface> tfReference = typefaces.get(assetName);
if (tfReference == null || tfReference.get() == null) {
final Typeface tf = Typeface.createFromAsset(context.getResources().getAssets(),
assetName);
typefaces.put(assetName, new WeakReference<Typeface>(tf));
return tf;
}
return tfReference.get();
}
}
通过这种方式,您可以创建一个继承自TextView的视图,并在其构造函数上调用setTypeface。
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在Android中将字符串转换为Uri
- 如何在NestedScrollView内使用RecyclerView ?
- 移动到另一个EditText时,软键盘下一步点击Android
- Android应用中的GridView VS GridLayout
- Activity和FragmentActivity的区别
- 右对齐文本在android TextView
- 权限拒绝:start前台需要android.permission.FOREGROUND_SERVICE
- 如何更改android操作栏的标题和图标
- Android Split字符串
- 让一个链接在安卓浏览器启动我的应用程序?
- 如何在Android工作室的外部库中添加一个jar ?
- GridLayout(不是GridView)如何均匀地拉伸所有子元素
- 如何让一个片段删除自己,即它的等效完成()?