我应用了一个自定义字体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);
谁能帮我摆脱这个问题?
当前回答
如果你想从网络加载字体或轻松地设置它的样式,你可以使用:
https://github.com/shellum/fontView
例子:
<!--Layout-->
<com.finalhack.fontview.FontView
android:id="@+id/someFontIcon"
android:layout_width="80dp"
android:layout_height="80dp" />
//Java:
fontView.setupFont("http://blah.com/myfont.ttf", true, character, FontView.ImageType.CIRCLE);
fontView.addForegroundColor(Color.RED);
fontView.addBackgroundColor(Color.WHITE);
其他回答
你可以在https://github.com/neopixl/PixlUI上使用PixlUI
导入他们的.jar并在XML中使用它
<com.neopixl.pixlui.components.textview.TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
pixlui:typeface="GearedSlab.ttf" />
确保在调用super和setContentView()之后将上述代码粘贴到onCreate()中。这个小细节让我挂念了一会儿。
随着Android 8.0在应用程序中使用自定义字体变得容易下载字体。 我们可以直接将字体添加到项目文件夹中的res/font/文件夹中,这样一来,字体就会自动在Android Studio中可用。
现在将fontFamily属性设置为字体列表或单击更多并选择您选择的字体。这将添加tools:fontFamily="@font/your_font_file"行到你的TextView。
这将自动生成几个文件。
1. 在values文件夹中,它将创建fonts_certs.xml。
2. 在Manifest中,它会添加这样的行:
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
3. preloaded_fonts.xml
<resources>
<array name="preloaded_fonts" translatable="false">
<item>@font/open_sans_regular</item>
<item>@font/open_sans_semibold</item>
</array>
</resources>
在Android O预览版中,最好的方法是: 它只适用于android studio-2.4或更高版本
右键单击res文件夹,进入New > Android资源目录。新 显示“资源目录”窗口。 在“资源类型”列表中选择字体,然后单击“确定”。 在字体文件夹中添加您的字体文件。下面的文件夹结构生成R.font。dancing_script R.font。la_la和R.font.ba_ba。 双击字体文件,在编辑器中预览文件的字体。
接下来,我们必须创建一个字体族:
右键单击字体文件夹,选择新建>字体资源文件。出现“新建资源文件”窗口。 输入文件名,然后单击OK。新的字体资源XML在编辑器中打开。 将每个字体文件、样式和权重属性包含在字体标记元素中。下面的XML演示了如何在字体资源XML中添加与字体相关的属性:
添加字体到TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/hey_fontfamily"/>
从文档来看
使用字体
所有步骤都是正确的。
由于我对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;
}
}