Java 自定义字体和XML布局(Android)
我试图在Android中使用XML文件定义GUI布局。据我所知,没有办法指定您的小部件在XML文件中使用自定义字体(例如,您在assets/font/中放置的字体),您只能使用系统安装的字体 我知道,在Java代码中,我可以使用唯一的ID手动更改每个小部件的字体。或者,我可以迭代Java中的所有小部件来进行此更改,但这可能会非常缓慢Java 自定义字体和XML布局(Android),java,android,user-interface,interface,fonts,Java,Android,User Interface,Interface,Fonts,我试图在Android中使用XML文件定义GUI布局。据我所知,没有办法指定您的小部件在XML文件中使用自定义字体(例如,您在assets/font/中放置的字体),您只能使用系统安装的字体 我知道,在Java代码中,我可以使用唯一的ID手动更改每个小部件的字体。或者,我可以迭代Java中的所有小部件来进行此更改,但这可能会非常缓慢 我还有其他选择吗?有没有更好的方法来制作具有自定义外观的小部件?我并不特别想为我添加的每个新小部件手动更改字体。使用自定义字体的唯一方法是通过源代码 请记住,And
我还有其他选择吗?有没有更好的方法来制作具有自定义外观的小部件?我并不特别想为我添加的每个新小部件手动更改字体。使用自定义字体的唯一方法是通过源代码
请记住,Android在资源和字体非常有限的设备上运行可能需要大量的RAM。内置Droid字体是专门制作的,如果您注意到的话,它缺少许多字符和装饰。扩展
TextView
并为其提供一个自定义属性,或者只使用android:tag属性传入您想要使用的字体的字符串。您需要选择一个约定并坚持它,例如我将把所有字体放在res/assets/fonts/文件夹中,以便您的TextView类知道在哪里可以找到它们。然后在构造函数中,您只需在超级调用之后手动设置字体 您不能扩展TextView来创建小部件或在小部件布局中使用小部件:
如我所知,您可以扩展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中)
下面。这是一个教程,向您展示如何设置自定义字体,如@peter所述:
它还考虑了潜在的内存泄漏。本教程中还有一个在按钮上设置自定义字体的示例。如果您只想添加一种字体,并且希望编写的代码更少,则可以为特定字体创建专用文本视图。请参阅下面的代码
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);
}
}
在main.xml中,您现在可以按如下方式添加文本视图:
<com.yourpackage.FontTextView
android:id="@+id/tvTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
Peter的答案是最好的,但可以通过使用Android的style.xml为应用程序中的所有文本视图自定义字体来改进
我的代码是这可能有点晚了,但您需要创建一个返回自定义字体的单例类,以避免内存泄漏
字体类别:
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;
}
}
自定义文本视图:
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());
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
**android:fontFamily="@font/ba_ba"**/>
通过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" />
我已经晚了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" />
对于这个问题,我可能会有一个简单的答案,而无需扩展TextView并实现一个长代码
代码:
<?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>
TextView tv = (TextView) findViewById(R.id.textview1);
tv.setTypeface(Typeface.createFromAsset(getAssets(), "font.ttf"));
像往常一样将自定义字体文件放在assets文件夹中,然后试试这个。它对我很有用。
我只是不明白为什么peter为这件简单的事情给出了如此庞大的代码,或者他给出了旧版本的答案。旧问题,但我确实希望在我开始搜索一个好的解决方案之前阅读这个答案。扩展安卓:fontFamily
属性,在您的资产文件夹中添加对自定义字体的支持,例如e因此:
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="fonts/Roboto-Bold.ttf"/>
您还可以指定自己的自定义属性来替换android:fontFamily
它也适用于主题,包括AppTheme。Fontinator是一个Android库,可轻松使用自定义字体。
使用数据绑定:
在XML中:
<TextView
app:font="@{`Source-Sans-Pro-Regular.ttf`}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
字体文件必须位于资产/font/
中,也可以在xml中定义,而无需创建自定义类
style.xml
<style name="ionicons" parent="android:TextAppearance">
<!-- Custom Attr-->
<item name="fontPath">fonts/ionicons.ttf</item>
</style>
字体/ionicons.ttf
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>
一个简短的提示,因为我总是忘记把字体放在哪里,它的字体必须在资产
中,并且这个文件夹位于res
和src
的同一级别,在我的例子中是它的资产/font/ionicons.ttf
更新了添加了根布局,因为此方法需要xmlns:app=”http://schemas.android.com/apk/res-auto“
开始工作
更新2忘记了我以前安装的一个名为的库,从Android O预览版开始,最好的方法就是这样做
1.)右键单击res文件夹,进入New>Android资源目录。新的
资源目录窗口出现。
2.)在资源类型列表中,选择font,然后单击确定。
3.在字体文件夹中添加字体文件。下面的文件夹结构生成R.font.dancing\u脚本、R.font.la\u la和R.font.ba\u ba。
4.双击字体文件以在编辑器中预览文件的字体
接下来,我们必须创建一个字体族
1.)右键单击字体文件夹并转到新建>字体资源文件。此时会出现“新建资源文件”窗口。
2.)输入文件名,然后单击确定。新的字体资源XML将在编辑器中打开。
3.)在字体标记元素中包含每个字体文件、样式和权重属性。以下XML说明了如何在字体资源XML中添加与字体相关的属性:
<?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>
向文本视图添加字体:
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());
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
**android:fontFamily="@font/ba_ba"**/>
从文件上看
所有步骤都正确。有两种自定义字体的方法:
!!!我在assets/fonts/iran_sans.ttf中的自定义字体
方式1:
Refrection Typeface.class | | |最佳方式
在扩展应用程序的类中调用FontsOverride.setDefaultFont(),此代码将导致所有软件字体发生更改,甚至会更改字体
AppCont
<?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"**/>
public class AppController extends Application {
@Override
public void onCreate() {
super.onCreate();
//Initial Font
FontsOverride.setDefaultFont(getApplicationContext(), "MONOSPACE", "fonts/iran_sans.ttf");
}
}
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();
}
}
}
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"));
}
}
public class FontUtils {
private static Hashtable<String, Typeface> fontCache = new Hashtable<>();
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;
}
}
public class CustomTextView extends AppCompatTextView {
private int mFont = FontUtils.FONTS_NORMAL;
boolean fontApplied;
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, context);
}
public CustomTextView(Context context) {
super(context);
init(null, context);
}
protected void init(AttributeSet attrs, Context cxt) {
if (!fontApplied) {
if (attrs != null) {
mFont = attrs.getAttributeIntValue(
"http://schemas.android.com/apk/res-auto", "Lato-Regular.ttf",
-1);
}
Typeface typeface = getTypeface();
int typefaceStyle = Typeface.NORMAL;
if (typeface != null) {
typefaceStyle = typeface.getStyle();
}
if (mFont > FontUtils.FONTS) {
typefaceStyle = mFont;
}
FontUtils.applyFont(this, typefaceStyle);
fontApplied = true;
}
}
}
public class FontUtils {
public static final int FONTS = 1;
public static final int FONTS_NORMAL = 2;
public static final int FONTS_BOLD = 3;
public static final int FONTS_BOLD1 = 4;
private static Map<String, Typeface> TYPEFACE = new HashMap<String, Typeface>();
static Typeface getFonts(Context context, String name) {
Typeface typeface = TYPEFACE.get(name);
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(), name);
TYPEFACE.put(name, typeface);
}
return typeface;
}
public static void applyFont(TextView tv, int typefaceStyle) {
Context cxt = tv.getContext();
Typeface typeface;
if(typefaceStyle == Typeface.BOLD_ITALIC) {
typeface = FontUtils.getFonts(cxt, "FaktPro-Normal.ttf");
}else if (typefaceStyle == Typeface.BOLD || typefaceStyle == SD_FONTS_BOLD|| typefaceStyle == FONTS_BOLD1) {
typeface = FontUtils.getFonts(cxt, "FaktPro-SemiBold.ttf");
} else if (typefaceStyle == Typeface.ITALIC) {
typeface = FontUtils.getFonts(cxt, "FaktPro-Thin.ttf");
} else {
typeface = FontUtils.getFonts(cxt, "FaktPro-Normal.ttf");
}
if (typeface != null) {
tv.setTypeface(typeface);
}
}
}