Android 中的自定义字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3203694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom Fonts in Android
提问by Paresh Mayani
I have already read some articles and searched on Google, but I failed to do it.
我已经阅读了一些文章并在谷歌上搜索,但我没有做到。
My problem is regarding the font-face.
我的问题是关于字体的。
In Android, there are only 4 attributes in "android:typeface"
: Normal, Sans, Serif, Monospace.
在 Android 中,只有 4 个属性"android:typeface"
:Normal、Sans、Serif、Monospace。
So what do I have to do to use "Verdana" in my application?
那么我必须做什么才能在我的应用程序中使用“Verdana”?
Please suggest me a correct way to use this font in my Android application.
请建议我在我的 Android 应用程序中使用这种字体的正确方法。
回答by Cristian
This is a simple example... create a folder in the root of your project called assets/fonts/
then paste the TTF font file (in this case Verdana.ttf). Then, if you want to apply that font to, say a TextView
, do the following:
这是一个简单的示例...在项目的根目录中创建一个名为的文件夹,assets/fonts/
然后粘贴 TTF 字体文件(在本例中为 Verdana.ttf)。然后,如果您想将该字体应用于 a TextView
,请执行以下操作:
import android.graphics.Typeface;
public class FontSampler extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),
"fonts/Verdana.ttf");
tv.setTypeface(face);
}
}
This example was taken from the ComonsWare book (written by Mark Murphy). You can download the full example from GitHub.
此示例取自 ComonsWare 一书(由 Mark Murphy 编写)。您可以从 GitHub 下载完整示例。
回答by guest2343sdfdfs
You can use PixlUI at https://github.com/neopixl/PixlUI
您可以在https://github.com/neopixl/PixlUI使用 PixlUI
import their .jar and use it in XML
导入他们的 .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" />
回答by PunitD
Well!!
This question is pretty old but still if someone is looking for the answer(in 2015) on how to apply custom font to all the Textviews through xml code directly see below:
好!!
这个问题已经很老了,但如果有人正在寻找关于如何通过 xml 代码将自定义字体应用于所有 Textviews 的答案(2015 年),请直接参见下文:
First:
we need to add custom font inside assets folder inside your app directory:
.ttfor .otfboth work in case of Android
首先:
我们需要在您的应用程序目录中的资产文件夹中添加自定义字体:
.ttf或.otf都适用于 Android
Second:
Create Class CustomTextView which extends TextView like below:
第二:
创建类 CustomTextView 扩展 TextView 如下:
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setTypeface(Typeface tf) {
super.setTypeface(FontCache.getFont(getContext(),"fonts/<font_name>"));
}
}
Third:
FontCache class being used inside CustomTextView's setTypeface() method.Purpose is to do basic Font Caching using HashMap:
第三:
在 CustomTextView 的 setTypeface() 方法中使用的 FontCache 类。目的是使用 HashMap 进行基本的字体缓存:
public class FontCache {
private static Map<String,Typeface> fontMap = new HashMap<String,Typeface>();
public static Typeface getFont(Context context,String fontname){
if(fontMap.containsKey(fontname)){
return fontMap.get(fontname);
}
else{
Typeface tf = Typeface.createFromAsset(context.getAssets(),fontname);
fontMap.put(fontname,tf);
return tf;
}
}
}
Fourth:[Final step] All we do now is use the CustomTextView directly inside our xml file wherever custom font textview is required:
第四:[最后一步] 我们现在要做的就是在需要自定义字体文本视图的地方直接在我们的 xml 文件中使用 CustomTextView:
<<package_name>.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Font Text"
android:textSize ="18sp"
android:textAppearance="?android:textAppearanceSmall"
android:id="@+id/custom_txt"
/>
Sorry, if this has already been posted somewhere on SO. Just thought to share if it helps someone!!
抱歉,如果这已经发布在 SO 上的某个地方。只是想分享如果它对某人有帮助!!
回答by Vijay Vankhede
You can use simple EasyFontsthird party library to set variety of custom font to your TextView
. By using this library you should not have to worry about downloading and adding fonts into the assets/fonts folder. Also about Typeface object creation.
您可以使用简单的EasyFonts第三方库为您的TextView
. 通过使用这个库,您不必担心下载字体并将其添加到 assets/fonts 文件夹中。还有关于字体对象的创建。
This library does not provides Verdana Font face.
这个库不提供 Verdana 字体。
But provide following font faces. Which might you would like to use.
但提供以下字体。您可能想使用哪个。
- Roboto
- Droid Serif
- Droid Robot
- Freedom
- Fun Raiser
- Android Nation
- Green Avocado
- Recognition
- 机器人
- 机器人衬线
- 机器人机器人
- 自由
- 有趣的提升者
- 安卓国度
- 绿色鳄梨
- 认出
Simply:
简单地:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
I am author of this library.
我是这个图书馆的作者。
回答by Stefan Medack
To change the (custom) font of your app globally, have a look at Calligraphy
要全局更改应用程序的(自定义)字体,请查看Calligraphy
Simply add Calligraphy to your gradle.build and add the following snippet to your Application.onCreate()
:
只需将 Calligraphy 添加到您的 gradle.build 并将以下代码段添加到您的Application.onCreate()
:
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/MyCustomFont.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
and in every Activity add the following:
并在每个活动中添加以下内容:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
That is all you need to do to change the font globally in your App. Have a look at the docs for more details.
这就是在应用程序中全局更改字体所需的全部操作。查看文档以获取更多详细信息。
回答by Vinod Joshi
// My example show you how to change fonts into a normal textView or list view
create a fonts folder into your assets dir of android and copy your custom font in that ..
assets/fonts/monaco.ttf
// Font path
String fontPath = "fonts/monaco.ttf";
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// CASE 1 : Inside your list view
holder.name = (TextView) convertView
.findViewById(R.id.textView_cityName);
// set name of text in each row
holder.name.setText(CitiesNames.get(position));
// set the type of font you want to set
holder.name.setTypeface(tf);
// CASE 2 : Inside your text view
TextView tx = (TextView)findViewById(R.id.textview1);
tx.setTypeface(tf);
//vKj
回答by saman
TextView textView = (Textview) findViewById(R.id.mytext);
Typeface face=Typeface.createFromAsset(getAssets(),
"fonts/Verdana.ttf");
textView.setTypeFace(face);