Android使用自定义字体

我将用户字体应用于
TextView

, 但他似乎没有改变字体。

这是我的代码:


Typeface myTypeface = Typeface.createFromAsset/getAssets//, "fonts/myFont.ttf"/;
TextView myTextView = /TextView/findViewById/R.id.myTextView/;
myTextView.setTypeface/myTypeface/;


有人可以把我拉出这个问题吗?
已邀请:

诸葛浮云

赞同来自:

在 Mobiletuts+ 有一个非常好的文本格式的教科书 Android.
http://code.tutsplus.com/tutor ... 1601/
Android

EDIT: 现在我自己检查了它。 这是一个解决方案。 您可以使用子文件夹 fonts, 但它应该在文件夹中
assets

, 不在文件夹中
res

. 所以

资产 / 字体

还要确保字体的结尾,我的意思是字体文件本身的结尾,所有小写字母。 换句话说,它不应该是
myFont.TTF

, 但
myfont.ttf


此路径必须在小写中

涵秋

赞同来自:

尝试了大多数在本主题中描述的解决方案,我不小心发现了书法 /
https://github.com/chrisjenx/Calligraphy
/ - 克里斯托弗图书馆 Jenkins, 这使得可以轻松地将自定义字体添加到您的应用程序中。 它的优势 lib 与此处提供的方法相比,如下:

您无需输入自己的重新定义组件。 TextView, 您使用内置组件 TextView

您可以轻松启用库使用 gradle

库不限制您的字体选择; 您只需将您的首选字体添加到目录中。 assets

您不仅获取自定义文本视图 — 所有其他文本组件 Android 也使用您的自定义字体显示。

冰洋

赞同来自:

我知道已经有很好的答案,但这是一个完全工作的实现。

以下是一种自定义文本表单:


package com.mycompany.myapp.widget;

/**
* Text view with a custom font.
* <p></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="[url=http://schemas.android.com/apk/res-auto"]http://schemas.android.com/apk/res-auto"[/url]} in the header.
*/
public class CustomFontTextView extends TextView {

private static final String sScheme =
"[url=http://schemas.android.com/apk/res-auto";]http://schemas.android.com/apk/res-auto";[/url]
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 format="string" name="customFont"></attr>
</declare-styleable>
</resources>


这就是你用它的方式。 我将使用相对布局包裹它并显示公告
customAttr

, 但显然,这可能是您已经拥有的任何布局。


<relativelayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="[url=http://schemas.android.com/apk/res/android"]http://schemas.android.com/apk/res/android"[/url] xmlns:customattrs="[url=http://schemas.android.com/apk/res-auto"]http://schemas.android.com/apk/res-auto"[/url] xmlns:tools="[url=http://schemas.android.com/tools">]http://schemas.android.com/tools">[/url]
<com.mycompany.myapp.widget.customfonttextview android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="foobar" customattrs:customfont="roboto_thin"></com.mycompany.myapp.widget.customfonttextview>
</relativelayout>

奔跑吧少年

赞同来自:

我以前成功使用过。 我们实现的唯一区别在于我没有使用子文件夹 assets. 虽然不确定这将改变一些东西。

奔跑吧少年

赞同来自:

如果您将字体放在正确的位置,并且字体文件本身没有错误,您的代码应该这样工作 RATTLESNAKE.

但是,如果您只需在布局中确定字体,则会更容易 xml, 例如,所以:


<linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" tools:context=".MainActivity" xmlns:android="[url=http://schemas.android.com/apk/res/android"]http://schemas.android.com/apk/res/android"[/url] xmlns:app="[url=http://schemas.android.com/apk/res-auto"]http://schemas.android.com/apk/res-auto"[/url] xmlns:tools="[url=http://schemas.android.com/tools">]http://schemas.android.com/tools">[/url]
<!-- This text view is styled with the app theme -->
<com.innovattic.font.fonttextview android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="This uses my font in bold italic style"></com.innovattic.font.fonttextview>
<!-- This text view is styled here and overrides the app theme -->
<com.innovattic.font.fonttextview android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="This uses another font in normal style" android:textstyle="normal" app:flfont="anotherFont"></com.innovattic.font.fonttextview>
<!-- This text view is styled with a style and overrides the app theme -->
<com.innovattic.font.fonttextview android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="This also uses another font in normal style" style="@style/StylishFont"></com.innovattic.font.fonttextview>
</linearlayout>


随着陪同
res/values/styles.xml

:


xml version="1.0" encoding="utf-8"?
<resources xmlns:android="[url=http://schemas.android.com/apk/res/android"]http://schemas.android.com/apk/res/android"[/url] xmlns:tools="[url=http://schemas.android.com/tools">]http://schemas.android.com/tools">[/url]
<!-- Application theme -->
<!-- Use a different parent if you don't want Holo Light -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:textViewStyle">@style/MyTextViewStyle</item>
</style>
<!-- Style to use for ALL text views /including FontTextView/ -->
<!-- Use a different parent if you don't want Holo Light -->
<style name="MyTextViewStyle" parent="@android:style/Widget.Holo.Light.TextView">
<item name="android:textAppearance">@style/MyTextAppearance</item>
</style>
<!-- Text appearance to use for ALL text views /including FontTextView/ -->
<!-- Use a different parent if you don't want Holo Light -->
<style name="MyTextAppearance" parent="@android:style/TextAppearance.Holo">
<!-- Alternatively, reference this font with the name "aspergit" -->
<!-- Note that only our own TextView's will use the font attribute -->
<item name="flFont">someFont</item>
<item name="android:textStyle">bold|italic</item>
</style>
<!-- Alternative style, maybe for some other widget -->
<style name="StylishFont">
<item name="flFont">anotherFont</item>
<item name="android:textStyle">normal</item>
</style>
</resources>


我为此目的创建了几个工具。 谈到
https://github.com/Innovattic/ ... ation
的 GitHub 或者看看这个
http://blog.innovattic.com/int ... roid/
这解释了这一切。

诸葛浮云

赞同来自:

弥补释放的最佳方式 Android 关于观看这种方式:

它仅适用于您 android studio-2.4 或更高

右键点击

文件夹 res

去吧

新资源目录 > Android

. 新的

将出现“资源目录”窗口。

在资源类型列表中,选择

字体

然后点击 OK.

将字体文件添加到字体文件夹。

下面的文件夹结构创建
R.font.dancing_script

,
R.font.la_la

, 和
R.font.ba_ba

.

双击

字体文件要在编辑器中查看文件字体。

接下来,我们必须创建一个字体系列:

右键单击“字体文件夹”并选择

创建 > 字体资源文件

. 将打开一个新的资源文件窗口。

进入

文件名并单击 OK

. 在编辑器中打开新的字体资源。 XML.

在字体标记的元素中插入每个字体文件,样式属性和权重。 下列的 XML 图示了添加与字体相关联的属性到字体资源 XML:


添加字体B. TextView:


<textview android:fontfamily="@font/hey_fontfamily" android:layout_height="wrap_content" android:layout_width="wrap_content"></textview>


如文档所示

https://developer.android.com/ ... .html
所有步骤都是真的。

三叔

赞同来自:

对于用户字体 android 在文件夹中创建一个文件夹 assets Folder name it "fonts" 将必要的文件放入其中。 fonts.ttf 或者 .otf.

如果你延伸 UIBaseFragment:


Typeface font = Typeface.createFromAsset/getActivity//.getAssets//, "fonts/Arial.ttf"/;
tv.setTypeface/font/;


即使活动正在扩展:


Typeface font = Typeface.createFromAsset/getContext//.getAssets//, "fonts/Arial.ttf"/;
tv.setTypeface/font/;

小明明

赞同来自:

您可以使用 PixlUI 在
https://github.com/neopixl/PixlUI
导入它们 .jar 并使用它 XML


<com.neopixl.pixlui.components.textview.textview android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/hello_world" pixlui:typeface="GearedSlab.ttf"></com.neopixl.pixlui.components.textview.textview>

诸葛浮云

赞同来自:

不幸的是,没有良好的解决方案。

我已经看到了许多关于使用定制的文章 TextView, 但他们忘记了这些不仅可以实现字体的文本演示。 & 在开发人员无法访问的其他想法中存在文本表示; 我甚至不开始合作

Spannable

.

您可以使用外部字体实用程序,例如:

http://github.com/chrisjenx/Calligraphy


它在创建时的应用程序中的每个视图上停靠,甚至这种实用程序都会错过一些视图。 /ViewPager 呈现普通的字体/, 然后你有一个问题,即何时 Google 更新您的装配工具,有时会导致失败,因为他需要瞄准过时的属性。 因为它使用,他也很慢

反射

Java .

它真的取决于 Google, 要解决这个问题。 我们需要最适合字体的支持 Android. 如果你看出决定 iOS, 然后他们真的有 100 内置字体可供选择。 想要你自己的字体吗? 只是戒烟 TFF, 它适合使用..

目前我们仅限于为我们提供的提案 Google, 这是非常有限的,但幸运的是,针对移动设备进行了优化。

喜特乐

赞同来自:

由于我对所呈现的所有决定并不满意 SO, 我自己想起了。 它基于一个带标签的小技巧。 /也就是说,您无法在代码中使用标签/, 我把一个字体路径放在那里。 因此,在定义视图时,您可以执行以下操作:


<textview android:id="@+id/textViewHello1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:tag="fonts/Oswald-Regular.ttf" android:text="Hello World 1"></textview>


或者那个:


<textview android:id="@+id/textViewHello2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Hello World 2" style="@style/OswaldTextAppearance"></textview>
<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, 对于活动和碎片的辅助方法:


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&lt;&gt;//;

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 &gt;= 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;
}
}


</string,>

石油百科

赞同来自:

请务必插入上面的代码 onCreate//



你对老板的召唤并打电话 setContentView//. 迫使我挂断了这一点细节。

帅驴

赞同来自:

您可以使用 easy & simple
https://android-arsenal.com/details/1/2044
第三方库安装各种自定义字体
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//;


此库还提供以下脸部字体。

机器人

Droid与Serifs.

机器人机器人

自由

风扇雷亚尔

Android 国家

绿色鳄梨

认出

诸葛浮云

赞同来自:



Android 8.0

使用附录中的自定义字体变得简单
downloadable fonts

.
我们可以直接添加字体

res/font/ folder


在项目文件夹中,同时有字体自动可用 Android Studio.

https://i.stack.imgur.com/GbkuY.png
现在安装

属性
fontFamily

在字体列表中,或单击按钮也根据您的选择选择字体。 它

添加字符串
tools:fontFamily="@font/your_font_file"

对你的 TextView.


这将自动创建多个文件。


1 .

在文件夹中 values 他会创造
fonts_certs.xml

.

2.

在清单中,他将添加这些行:


<meta-data android:name="preloaded_fonts" android:resource="@array/preloaded_fonts"></meta-data>


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>

冰洋

赞同来自:

如果您想从网络下载字体或易于体现它,可以使用:

https://github.com/shellum/fontView
例子:


Layout
<com.finalhack.fontview.fontview android:id="@+id/someFontIcon" android:layout_height="80dp" android:layout_width="80dp"></com.finalhack.fontview.fontview>

//Java:
fontView.setupFont/"[url=http://blah.com/myfont.ttf"]http://blah.com/myfont.ttf"[/url], true, character, FontView.ImageType.CIRCLE/;
fontView.addForegroundColor/Color.RED/;
fontView.addBackgroundColor/Color.WHITE/;

江南孤鹜

赞同来自:

我有同样的问题: TTF 没有出现。 我更改了字体文件,并使用相同的代码工作。

小明明

赞同来自:

响应更新:

Android 8.0 /API 等级 26/ 介绍一个新功能,字体 XML.
只需使用字体函数 XML 在运行的设备上 Android 4.1 /等级 16 API/ 更高,使用支持库 26.

看到这一点
https://developer.android.com/ ... n-xml

旧答案

配置字体有两种方法 :

!!! 我的自定义字体
assets/fonts/iran_sans.ttf


方法 1 :

Refrection Typeface.class |||

最好的方式

称呼 FontsOverride.setDefaultFont// 在课堂里 extends Application, 此代码将更改所有程序字体,即使是多士字体

AppController.java


public class AppController extends Application {

@Override
public void onCreate// {
super.onCreate//;

//Initial Font
FontsOverride.setDefaultFont/getApplicationContext//, "MONOSPACE", "fonts/iran_sans.ttf"/;

}
}


FontsOverride.java


public class FontsOverride {

public static void setDefaultFont/Context context, String staticTypefaceFieldName, String fontAssetName/ {
final Typeface regular = Typeface.createFromAsset/context.getAssets//, fontAssetName/;
replaceFont/staticTypefaceFieldName, regular/;
}

private static void replaceFont/String staticTypefaceFieldName, final Typeface newTypeface/ {
try {
final Field staticField = Typeface.class.getDeclaredField/staticTypefaceFieldName/;
staticField.setAccessible/true/;
staticField.set/null, newTypeface/;
} catch /NoSuchFieldException e/ {
e.printStackTrace//;
} catch /IllegalAccessException e/ {
e.printStackTrace//;
}
}
}



方法 2:

使用 setTypeface

特别查看只是打电话 setTypeface//, 更改字体。

CTextView.java


public class CTextView extends TextView {

public CTextView/Context context/ {
super/context/;
init/context,null/;
}

public CTextView/Context context, @Nullable AttributeSet attrs/ {
super/context, attrs/;
init/context,attrs/;
}

public CTextView/Context context, @Nullable AttributeSet attrs, int defStyleAttr/ {
super/context, attrs, defStyleAttr/;
init/context,attrs/;
}

@RequiresApi/api = Build.VERSION_CODES.LOLLIPOP/
public CTextView/Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes/ {
super/context, attrs, defStyleAttr, defStyleRes/;
init/context,attrs/;
}

public void init/Context context, @Nullable AttributeSet attrs/ {

if /isInEditMode///
return;

// use setTypeface for change font this view
setTypeface/FontUtils.getTypeface/"fonts/iran_sans.ttf"//;

}
}


FontUtils.java


public class FontUtils {

private static Hashtable<string, typeface=""> fontCache = new Hashtable&lt;&gt;//;

public static Typeface getTypeface/String fontName/ {
Typeface tf = fontCache.get/fontName/;
if /tf == null/ {
try {
tf = Typeface.createFromAsset/AppController.getInstance//.getApplicationContext//.getAssets//, fontName/;
} catch /Exception e/ {
e.printStackTrace//;
return null;
}
fontCache.put/fontName, tf/;
}
return tf;
}

}


</string,>

莫问

赞同来自:

嗯,在七年中,您可以轻松更改整个应用程序
textView

或者您想要使用库
android.support

26++.

E.g:

创建你的包裹

字体

app/src/res/font 并将您的字体传递给它。

https://i.stack.imgur.com/gCE99.png
在您的应用程序中,只需将其添加为 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 Jelly Bean

, 这是一个广泛的范围。

小明明

赞同来自:

这样做的正确方法 API 26 这里的官方文件中描述 :

https://developer.android.com/ ... .html
这包括文件的位置。 ttf 在文件夹中 res/font 并创建字体系列文件。

冰洋

赞同来自:

打开您的项目并选择

项目

在左上角

app

-->

src

-->

main

右键单击主要并创建目录调用它

资产

右键单击以评估并创建一个新目录,名称。

字体

您需要查找免费字体,例如
https://www.1001freefonts.com/italic-fonts.php
把它交给你自己的 Textview 并在其类别的活动中命名

将字体复制到文件夹

fonts


TextView txt = /TextView/ findViewById/R.id.txt_act_spalsh_welcome/;
Typeface font = Typeface.createFromAsset/getAssets//, "fonts/Aramis Italic.ttf"/;
txt.setTypeface/font/;


字体名称必须正确且开朗。

三叔

赞同来自:

是的,如上所述,下载的字体非常简单

Dipli S.

这就是你的方式......

放置A.
TextView

.

在“属性”面板上,选择下拉列表。
fontFamily

. 如果不是那里,找到马车 />然后单击它以部署
textAppearance

/ 在它下面。

展开下拉列表
font-family

.

在一个小小的列表中,向下滚动一切,直到看到
more fonts


这将打开一个对话框,您可以在其中搜索
Google Fonts


找到顶部的搜索栏中需要的字体

选择您的字体。

选择您喜欢的字体样式 /例如,粗体,通常,斜体等。/

在右窗格中,选择带有铭文的交换机
Add font to project


单击确定。 现在是你的 TextView 它有你喜欢的字体!

BONUS:
如果你想要程式化 EVERYTHING 使用所选字体的应用程序中的文本,只需添加
<item name="android:fontfamily">@font/fontnamehere</item>

在你的中
styles.xml

裸奔

赞同来自:

最简单的解决方案 android 现在支持!

使用用户字体 xml:


<textview android:fontfamily="@font/[your font resource]" android:layout_height="wrap_content" android:layout_width="wrap_content"></textview>


看起来更多:

https://developer.android.com/ ... .html

要回复问题请先登录注册