我应用了一个自定义字体TextView,但它似乎没有改变字体。
这是我的代码:
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(myTypeface);
谁能帮我摆脱这个问题?
我应用了一个自定义字体TextView,但它似乎没有改变字体。
这是我的代码:
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(myTypeface);
谁能帮我摆脱这个问题?
当前回答
由于我对SO的所有解决方案都不满意,所以我提出了我的解决方案。这是基于标签的一个小技巧(即你不能在你的代码中使用标签),我把字体路径放在那里。所以当定义视图时,你可以这样做:
<TextView
android:id="@+id/textViewHello1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World 1"
android:tag="fonts/Oswald-Regular.ttf"/>
或:
<TextView
android:id="@+id/textViewHello2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World 2"
style="@style/OswaldTextAppearance"/>
<style name="OswaldTextAppearance">
<item name="android:tag">fonts/Oswald-Regular.ttf</item>
<item name="android:textColor">#000000</item>
</style>
现在你可以显式地访问/设置视图如下:
TextView textView = TextViewHelper.setupTextView(this, R.id.textViewHello1).setText("blah");
或者只是通过:
TextViewHelper.setupTextViews(this, (ViewGroup) findViewById(R.id.parentLayout)); // parentLayout is the root view group (relative layout in my case)
你问的魔法课是什么?主要是从另一个SO帖子中粘来的,为活动和片段提供了helper方法:
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.HashMap;
import java.util.Map;
public class TextViewHelper {
private static final Map<String, Typeface> mFontCache = new HashMap<>();
private static Typeface getTypeface(Context context, String fontPath) {
Typeface typeface;
if (mFontCache.containsKey(fontPath)) {
typeface = mFontCache.get(fontPath);
} else {
typeface = Typeface.createFromAsset(context.getAssets(), fontPath);
mFontCache.put(fontPath, typeface);
}
return typeface;
}
public static void setupTextViews(Context context, ViewGroup parent) {
for (int i = parent.getChildCount() - 1; i >= 0; i--) {
final View child = parent.getChildAt(i);
if (child instanceof ViewGroup) {
setupTextViews(context, (ViewGroup) child);
} else {
if (child != null) {
TextViewHelper.setupTextView(context, child);
}
}
}
}
public static void setupTextView(Context context, View view) {
if (view instanceof TextView) {
if (view.getTag() != null) // also inherited from TextView's style
{
TextView textView = (TextView) view;
String fontPath = (String) textView.getTag();
Typeface typeface = getTypeface(context, fontPath);
if (typeface != null) {
textView.setTypeface(typeface);
}
}
}
}
public static TextView setupTextView(View rootView, int id) {
TextView textView = (TextView) rootView.findViewById(id);
setupTextView(rootView.getContext().getApplicationContext(), textView);
return textView;
}
public static TextView setupTextView(Activity activity, int id) {
TextView textView = (TextView) activity.findViewById(id);
setupTextView(activity.getApplicationContext(), textView);
return textView;
}
}
其他回答
七年后,你可以改变整个应用程序的textView或你想要的很容易通过使用android。支持库26++。
E.g:
创建字体包app/src/res/font,并将字体移动到其中。
在你的应用主题中添加一个fontFamily:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
. . . ...
<item name="android:fontFamily">@font/demo</item>
</style>
仅用于textView的示例:
<style name="fontTextView" parent="@android:style/Widget.TextView">
<item name="android:fontFamily">monospace</item>
</style>
在你的主题中加入:
<item name="android:textViewStyle">@style/fontTextView</item>
目前它工作在8.1到4.1 API果冻豆,这是一个广泛的范围。
在Mobiletuts+上有一个关于Android文本格式的很好的教程。快速提示:自定义Android字体
编辑:现在我自己测试了一下。这是解决方案。你可以使用一个子文件夹称为字体,但它必须在资产文件夹,而不是在res文件夹。所以
资产/字体
还要确保字体结尾。我是说字体文件本身的结尾都是小写。换句话说,它不应该是myfont。ttf,而应该是myfont。ttf,这种方式必须小写
您可以使用简单的EasyFonts第三方库来设置各种自定义字体到您的TextView。通过使用这个库,你应该不必担心下载和添加字体到assets/fonts文件夹。还有关于字体对象的创建。
而不是
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(myTypeface);
简单:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
这个库还提供了以下字体面。
Roboto Droid衬线 机器人机器人 自由 有趣的栽培者 Android的国家 绿色的鳄梨 识别
由于我对SO的所有解决方案都不满意,所以我提出了我的解决方案。这是基于标签的一个小技巧(即你不能在你的代码中使用标签),我把字体路径放在那里。所以当定义视图时,你可以这样做:
<TextView
android:id="@+id/textViewHello1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World 1"
android:tag="fonts/Oswald-Regular.ttf"/>
或:
<TextView
android:id="@+id/textViewHello2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World 2"
style="@style/OswaldTextAppearance"/>
<style name="OswaldTextAppearance">
<item name="android:tag">fonts/Oswald-Regular.ttf</item>
<item name="android:textColor">#000000</item>
</style>
现在你可以显式地访问/设置视图如下:
TextView textView = TextViewHelper.setupTextView(this, R.id.textViewHello1).setText("blah");
或者只是通过:
TextViewHelper.setupTextViews(this, (ViewGroup) findViewById(R.id.parentLayout)); // parentLayout is the root view group (relative layout in my case)
你问的魔法课是什么?主要是从另一个SO帖子中粘来的,为活动和片段提供了helper方法:
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.HashMap;
import java.util.Map;
public class TextViewHelper {
private static final Map<String, Typeface> mFontCache = new HashMap<>();
private static Typeface getTypeface(Context context, String fontPath) {
Typeface typeface;
if (mFontCache.containsKey(fontPath)) {
typeface = mFontCache.get(fontPath);
} else {
typeface = Typeface.createFromAsset(context.getAssets(), fontPath);
mFontCache.put(fontPath, typeface);
}
return typeface;
}
public static void setupTextViews(Context context, ViewGroup parent) {
for (int i = parent.getChildCount() - 1; i >= 0; i--) {
final View child = parent.getChildAt(i);
if (child instanceof ViewGroup) {
setupTextViews(context, (ViewGroup) child);
} else {
if (child != null) {
TextViewHelper.setupTextView(context, child);
}
}
}
}
public static void setupTextView(Context context, View view) {
if (view instanceof TextView) {
if (view.getTag() != null) // also inherited from TextView's style
{
TextView textView = (TextView) view;
String fontPath = (String) textView.getTag();
Typeface typeface = getTypeface(context, fontPath);
if (typeface != null) {
textView.setTypeface(typeface);
}
}
}
}
public static TextView setupTextView(View rootView, int id) {
TextView textView = (TextView) rootView.findViewById(id);
setupTextView(rootView.getContext().getApplicationContext(), textView);
return textView;
}
public static TextView setupTextView(Activity activity, int id) {
TextView textView = (TextView) activity.findViewById(id);
setupTextView(activity.getApplicationContext(), textView);
return textView;
}
}
在尝试了这篇文章中描述的大多数解决方案后,我偶然发现了Calligraphy (https://github.com/chrisjenx/Calligraphy) - Christopher Jenkins的一个库,可以让你轻松地向应用程序添加自定义字体。与这里建议的方法相比,他的库的优点是:
你不必引入自己的重载TextView组件,你使用内置的TextView 您可以使用gradle轻松地包含该库 图书馆不限制你的字体选择;您只需将您喜欢的添加到资产目录 你不仅得到自定义文本视图-所有其他基于文本的Android组件也将显示使用您的自定义字体。