所以我想在android中更改android:fontFamily,但我在android中没有看到任何预定义的字体。如何选择预定义的选项之一?我真的不需要定义我自己的TypeFace,但我所需要的是与现在显示的不同。
<TextView
android:id="@+id/HeaderText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp"
android:gravity="center"
android:text="CallerBlocker"
android:textSize="40dp"
android:fontFamily="Arial"
/>
看来我在上面做的事真的行不通!BTW android:fontFamily=“Arial”是一个愚蠢的尝试!
我使用Letter Press库来处理我的非文本视图内容,如Buttons和kianoni fontloader库处理我的文本视图,因为在这个库中使用样式比Letter Press更容易,我得到了理想的反馈。这对于那些想要使用除Roboto字体之外的自定义字体的人来说非常有用。这是我使用字体库的经验。对于那些希望使用自定义类更改字体的人,我强烈建议使用以下代码段创建此类
public class TypefaceSpan extends MetricAffectingSpan {
/** An <code>LruCache</code> for previously loaded typefaces. */
private static LruCache<String, Typeface> sTypefaceCache =
new LruCache<String, Typeface>(12);
private Typeface mTypeface;
/**
* Load the {@link android.graphics.Typeface} and apply to a {@link android.text.Spannable}.
*/
public TypefaceSpan(Context context, String typefaceName) {
mTypeface = sTypefaceCache.get(typefaceName);
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getApplicationContext()
.getAssets(), String.format("fonts/%s", typefaceName));
// Cache the loaded Typeface
sTypefaceCache.put(typefaceName, mTypeface);
}
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
并像这样使用类:
AppData = PreferenceManager.getDefaultSharedPreferences(this);
TextView bannertv= (TextView) findViewById(R.id.txtBanner);
SpannableString s = new SpannableString(getResources().getString(R.string.enterkey));
s.setSpan(new TypefaceSpan(this, AppData.getString("font-Bold",null)), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
bannertv.setText(s);
也许这有帮助。
一种简单的方法是在项目中添加所需的字体。
转到文件->新建->新建资源目录选择字体
这将在资源中创建一个新目录,字体。
下载字体(.ttf)。我使用https://fonts.google.com同样的
将其添加到字体文件夹中,然后在XML中或以编程方式使用它们。
XML格式-
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/your_font"/>
程序性地-
Typeface typeface = ResourcesCompat.getFont(this, R.font.your_font);
textView.setTypeface(typeface);
要按程序设置字体,请写。。。
TextView tv7 = new TextView(this);
tv7.setText(" TIME ");
tv7.setTypeface(Typeface.create("sans-serif-condensed",Typeface.BOLD));
tv7.setTextSize(12);
tbrow.addView(tv7);
名称“sans-serif concentrated”是从fonts.xml文件中引用的,该文件应在app-->res-->values文件夹中创建,其中包含字体。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="font_family_light">sans-serif-light</string>
<string name="font_family_medium">sans-serif-medium</string>
<string name="font_family_regular">sans-serif</string>
<string name="font_family_condensed">sans-serif-condensed</string>
<string name="font_family_black">sans-serif-black</string>
<string name="font_family_thin">sans-serif-thin</string>
</resources>
希望这是明确的!