DrD*_*ost 174 java user-interface fonts android interface
我正在尝试使用Android中的XML文件定义GUI布局.据我所知,没有办法指定您的小部件应该在XML文件中使用自定义字体(例如,您放置在assets/font /中的字体),并且您只能使用系统安装的字体.
我知道,在Java代码中,我可以使用唯一ID手动更改每个窗口小部件的字体.或者,我可以迭代Java中的所有小部件来进行此更改,但这可能会非常慢.
我还有其他选择吗?有没有更好的方法来制作具有自定义外观的小部件?我不是特别想要为我添加的每个新小部件手动更改字体.
pet*_*ter 220
您可以扩展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;
}
}
Run Code Online (Sandbox Code Playgroud)
attrs.xml :(在res/values中)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextViewPlus">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
你会把"saxmono.ttf"放在assets文件夹中.
这种方法存在严重的记忆问题.请参阅下面的chedabob评论.
Rag*_*har 35
我参加晚会已经晚了3年:(但是对于那些可能偶然发现这篇文章的人来说,这可能会有用.
我编写了一个缓存Typefaces的库,并允许您直接从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" />
Run Code Online (Sandbox Code Playgroud)
Leo*_*oso 18
这可能有点晚,但您需要创建一个返回自定义字体的单例类,以避免内存泄漏.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
自定义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());
}
}
Run Code Online (Sandbox Code Playgroud)
通过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" />
Run Code Online (Sandbox Code Playgroud)
编程方式:
TextView programmaticallyTextView = (TextView)
findViewById(R.id.programmatically_text_view);
programmaticallyTextView.setTypeface(OpenSans.getInstance(this)
.getTypeFace());
Run Code Online (Sandbox Code Playgroud)
tho*_*ers 13
老问题,但在我开始自己寻找一个好的解决方案之前,我当然希望我在这里读到这个答案.书法扩展了android:fontFamily属性以在资产文件夹中添加对自定义字体的支持,如下所示:
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="fonts/Roboto-Bold.ttf"/>
Run Code Online (Sandbox Code Playgroud)
要激活它,您唯一需要做的就是将它附加到您正在使用的Activity的Context中:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(new CalligraphyContextWrapper(newBase));
}
Run Code Online (Sandbox Code Playgroud)
您还可以指定要替换的自定义属性 android:fontFamily
它也适用于主题,包括AppTheme.
使用DataBinding:
@BindingAdapter({"bind:font"})
public static void setFont(TextView textView, String fontName){
textView.setTypeface(Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/" + fontName));
}
Run Code Online (Sandbox Code Playgroud)
在XML中:
<TextView
app:font="@{`Source-Sans-Pro-Regular.ttf`}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)
字体文件必须在 assets/fonts/
如果你只有一个字体,你想补充,并想少写代码,您可以为您的特定字体专用的TextView.见下面的代码.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
在main.xml中,您现在可以像这样添加textView:
<com.yourpackage.FontTextView
android:id="@+id/tvTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
Run Code Online (Sandbox Code Playgroud)
小智 7
最好的方法从Android O预览版本就是这样
1.)右键单击res文件夹,然后转到新建> Android资源目录.将
出现"新建资源目录"窗口.
2.)在"资源类型"列表中,选择" 字体",然后单击"确定".
3.)在字体文件夹中添加字体文件.下面的文件夹结构生成R.font.dancing_script,R.font.la_la和R.font.ba_ba.
4.)双击字体文件以在编辑器中预览文件的字体.
接下来我们必须创建一个字体系列
1.)右键单击字体文件夹,然后转到" 新建">"字体资源文件".将出现"新建资源文件"窗口.
2.)输入文件名,然后单击"确定".新的字体资源XML在编辑器中打开.
3.)在font标签元素中包含每个字体文件,样式和权重属性.以下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>
Run Code Online (Sandbox Code Playgroud)
将字体添加到TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
**android:fontFamily="@font/ba_ba"**/>
Run Code Online (Sandbox Code Playgroud)
从文档中可以看出
所有步骤都是正确的.
扩展TextView并为其提供自定义属性,或者只使用android:tag属性传入要使用的字体的String.您将需要选择一个约定并坚持使用它,例如我将所有字体放在res/assets/fonts /文件夹中,以便TextView类知道在哪里找到它们.然后在构造函数中,您只需在超级调用后手动设置字体.
使用自定义字体的唯一方法是通过源代码。
请记住,Android 运行在资源非常有限的设备上,并且字体可能需要大量 RAM。内置的 Droid 字体是专门制作的,如果您注意到的话,会缺少许多字符和装饰。
| 归档时间: |
|
| 查看次数: |
166034 次 |
| 最近记录: |