我应用了一个自定义字体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);
谁能帮我摆脱这个问题?
当前回答
您可以使用简单的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的国家 绿色的鳄梨 识别
其他回答
确保在调用super和setContentView()之后将上述代码粘贴到onCreate()中。这个小细节让我挂念了一会儿。
你可以在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" />
您可以使用简单的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的国家 绿色的鳄梨 识别
我知道已经有了很好的答案,但这里有一个完全工作的实现。
这是自定义文本视图:
package com.mycompany.myapp.widget;
/**
* Text view with a custom font.
* <p/>
* In the XML, use something like {@code customAttrs:customFont="roboto-thin"}. The list of fonts
* that are currently supported are defined in the enum {@link CustomFont}. Remember to also add
* {@code xmlns:customAttrs="http://schemas.android.com/apk/res-auto"} in the header.
*/
public class CustomFontTextView extends TextView {
private static final String sScheme =
"http://schemas.android.com/apk/res-auto";
private static final String sAttribute = "customFont";
static enum CustomFont {
ROBOTO_THIN("fonts/Roboto-Thin.ttf"),
ROBOTO_LIGHT("fonts/Roboto-Light.ttf");
private final String fileName;
CustomFont(String fileName) {
this.fileName = fileName;
}
static CustomFont fromString(String fontName) {
return CustomFont.valueOf(fontName.toUpperCase(Locale.US));
}
public Typeface asTypeface(Context context) {
return Typeface.createFromAsset(context.getAssets(), fileName);
}
}
public CustomFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if (isInEditMode()) {
return;
} else {
final String fontName = attrs.getAttributeValue(sScheme, sAttribute);
if (fontName == null) {
throw new IllegalArgumentException("You must provide \"" + sAttribute + "\" for your text view");
} else {
final Typeface customTypeface = CustomFont.fromString(fontName).asTypeface(context);
setTypeface(customTypeface);
}
}
}
}
这里是自定义属性。这应该进入你的res/attrs.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomFontTextView">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
下面是你如何使用它。我将使用一个相对布局来包装它并显示customAttr声明,但它显然可以是您已经拥有的任何布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:customAttrs="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mycompany.myapp.widget.CustomFontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foobar"
customAttrs:customFont="roboto_thin" />
</RelativeLayout>
最简单的解决方案android支持现在!
使用自定义字体在xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/[your font resource]"/>
看详细信息:
https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html