我正在尝试在Android中使用XML文件定义GUI布局。据我所知,无法在XML文件中指定小部件应使用自定义字体(例如放置在assets / font /中的字体),您只能使用系统安装的字体。
我知道,在Java代码中,我可以使用唯一ID手动更改每个小部件的字体。或者,我可以在Java中迭代所有小部件以进行此更改,但这可能会非常慢。
我还有哪些选项?是否有更好的方法来制作具有自定义外观的小部件? 我不想为每个新添加的小部件手动更改字体。
我正在尝试在Android中使用XML文件定义GUI布局。据我所知,无法在XML文件中指定小部件应使用自定义字体(例如放置在assets / font /中的字体),您只能使用系统安装的字体。
我知道,在Java代码中,我可以使用唯一ID手动更改每个小部件的字体。或者,我可以在Java中迭代所有小部件以进行此更改,但这可能会非常慢。
我还有哪些选项?是否有更好的方法来制作具有自定义外观的小部件? 我不想为每个新添加的小部件手动更改字体。
根据我在这里学到的,你可以扩展TextView来设置自定义字体。
TextViewPlus.java:
package com.example;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class TextViewPlus extends TextView {
private static final String TAG = "TextView";
public TextViewPlus(Context context) {
super(context);
}
public TextViewPlus(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
attrs.xml:(在res/values目录下)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextViewPlus">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/com.example"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.TextViewPlus
android:id="@+id/textViewPlus1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="@string/showingOffTheNewTypeface"
foo:customFont="saxmono.ttf">
</com.example.TextViewPlus>
</LinearLayout>
你需要将 "saxmono.ttf" 放在 assets 文件夹中。
这种方法存在严重的内存问题。请参见下面chedabob的评论。
xmlns:foo="http://schemas.android.com/apk/res/com.example
。 - Theo我晚了3年才来参加派对 :( 然而这可能对于偶然发现此帖子的某人有所帮助。
我编写了一个缓存字体并允许您直接从XML指定自定义字体的库。您可以在这里找到该库。
以下是使用它时,您的XML布局的样子。
<com.mobsandgeeks.ui.TypefaceTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
geekui:customTypeface="fonts/custom_font.ttf" />
compile 'com.mobsandgeeks:android-typeface-textview:1.0'
但没成功。 - aimangoxmlns:geekui="http://schemas.android.com/apk/res-auto"
- Ragunath Jawahar这可能有点晚了,但是您需要创建一个单例类,以返回定制字体以避免内存泄漏。
TypeFace类:
public class OpenSans {
private static OpenSans instance;
private static Typeface typeface;
public static OpenSans getInstance(Context context) {
synchronized (OpenSans.class) {
if (instance == null) {
instance = new OpenSans();
typeface = Typeface.createFromAsset(context.getResources().getAssets(), "open_sans.ttf");
}
return instance;
}
}
public Typeface getTypeFace() {
return typeface;
}
}
自定义 TextView:
public class NativelyCustomTextView extends TextView {
public NativelyCustomTextView(Context context) {
super(context);
setTypeface(OpenSans.getInstance(context).getTypeFace());
}
public NativelyCustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setTypeface(OpenSans.getInstance(context).getTypeFace());
}
public NativelyCustomTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
setTypeface(OpenSans.getInstance(context).getTypeFace());
}
}
通过 XML:
<com.yourpackage.views.NativelyCustomTextView
android:id="@+id/natively_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:text="@string/natively"
android:textSize="30sp" />
编程方式:
TextView programmaticallyTextView = (TextView)
findViewById(R.id.programmatically_text_view);
programmaticallyTextView.setTypeface(OpenSans.getInstance(this)
.getTypeFace());
虽然这是一个老问题,但我真的希望在开始寻找好的解决方案之前能够在这里阅读到这个答案。 Calligraphy 扩展了 android:fontFamily
属性,以支持在您的资产文件夹中添加自定义字体,如下所示:
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="fonts/Roboto-Bold.ttf"/>
要激活它,您唯一需要做的就是将其附加到您正在使用的 Activity 的 Context 上:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(new CalligraphyContextWrapper(newBase));
}
你还可以指定自定义属性来替换android:fontFamily
它也适用于主题,包括AppTheme。
使用 DataBinding:
@BindingAdapter({"bind:font"})
public static void setFont(TextView textView, String fontName){
textView.setTypeface(Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/" + fontName));
}
在XML中:
<TextView
app:font="@{`Source-Sans-Pro-Regular.ttf`}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
字体文件必须在assets/fonts/
目录下。
package com.yourpackage;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class FontTextView extends TextView {
public static Typeface FONT_NAME;
public FontTextView(Context context) {
super(context);
if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/FontName.otf");
this.setTypeface(FONT_NAME);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/FontName.otf");
this.setTypeface(FONT_NAME);
}
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/FontName.otf");
this.setTypeface(FONT_NAME);
}
}
<com.yourpackage.FontTextView
android:id="@+id/tvTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/hey_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="@font/hey_bababa" />
</font-family>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
**android:fontFamily="@font/ba_ba"**/>
根据文档
所有步骤都是正确的。
扩展 TextView
并添加自定义属性,或者只需使用 android:tag 属性传递要使用的字体字符串。您需要选择一个约定并坚持使用,例如我将所有字体放在 res/assets/fonts/ 文件夹中,以便您的 TextView 类知道在哪里找到它们。然后在构造函数中,在 super 调用之后手动设置字体即可。
想要使用自定义字体,唯一的方式是通过源代码。
请记住,Android 运行在资源非常有限的设备上,字体可能需要大量的 RAM。内置的 Droid 字体是特别制作的,如果你注意到的话,会发现很多字符和装饰没有包含在内。
<style name="ionicons" parent="android:TextAppearance">
<!-- Custom Attr-->
<item name="fontPath">fonts/ionicons.ttf</item>
</style>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/ionicons"
android:text=""/>
</LinearLayout>
< p > 快速提示,因为我总是忘记在哪里放字体,它必须在assets
中,并且此文件夹与res
和src
处于同一级别,在我的情况下是assets/fonts/ionicons.ttf
更新 添加了根布局,因为此方法需要xmlns:app="http://schemas.android.com/apk/res-auto"
才能工作
更新2 忘记了一个我之前安装的库,叫做Calligraphy
xmlns:app="http://schemas.android.com/apk/res-auto"
添加到您的根布局中,同时检查更新后的答案。 - norman784