我应用了一个自定义字体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);
谁能帮我摆脱这个问题?
当前回答
我知道已经有了很好的答案,但这里有一个完全工作的实现。
这是自定义文本视图:
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>
其他回答
是的,就像Dipali说的那样,下载字体非常简单。
你是这样做的……
Place a TextView. In the properties pane, select the fontFamily dropdown. If it isn't there, find the caret thingy (the > and click on it to expand textAppearance) under the. Expand the font-family drop down. In the little list, scroll all the way down till you see more fonts This will open up a dialog box where you can search from Google Fonts Search for the font you like with the search bar at the top Select your font. Select the style of the font you like (i.e. bold, normal, italic, etc) In the right pane, choose the radio button that says Add font to project Click okay. Now your TextView has the font you like!
奖金: 如果你想在你的应用程序中使用所选字体的文本样式,只需添加<item name="android:fontfamily">@font/fontnamehere</item>到你的styles.xml
在尝试了这篇文章中描述的大多数解决方案后,我偶然发现了Calligraphy (https://github.com/chrisjenx/Calligraphy) - Christopher Jenkins的一个库,可以让你轻松地向应用程序添加自定义字体。与这里建议的方法相比,他的库的优点是:
你不必引入自己的重载TextView组件,你使用内置的TextView 您可以使用gradle轻松地包含该库 图书馆不限制你的字体选择;您只需将您喜欢的添加到资产目录 你不仅得到自定义文本视图-所有其他基于文本的Android组件也将显示使用您的自定义字体。
在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"/>
从文档来看
使用字体
所有步骤都是正确的。
我知道已经有了很好的答案,但这里有一个完全工作的实现。
这是自定义文本视图:
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>
你可以在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" />