所以我想在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”是一个愚蠢的尝试!


当前回答

如果你想编程,你可以使用

label.setTypeface(Typeface.SANS_SERIF, Typeface.ITALIC);

在SANS_SERIF中,可以使用:

违约默认_文件夹单空间桑塞里夫衬线

您可以在ITALIC中使用:

粗体粗体_斜体斜体正常的,正常的

所有这些都是在Android开发者上声明的

其他回答

以编程方式将字体添加到TextView的最简单方法是,首先,将字体文件添加到项目中的“资产”文件夹中。例如,字体路径如下所示:assets/fonts/my_font.otf

并将其添加到TextView中:

科特林

val font_path = "fonts/my_font.otf"  

myTypeface = Typeface.createFromAsset(MyApplication.getInstance().assets, font_path)

textView.typeface = myTypeface

Java

String font_path = "fonts/my_font.otf";
Typeface myTypeface = Typeface.createFromAsset(MyApplication.getInstance().assets, font_path)
textView.setTypeface(myTypeface);

你想要的是不可能的。您必须在代码中设置TypeFace。

在XML中,您可以做的是

android:typeface="sans" | "serif" | "monospace"

除此之外,您无法使用XML中的字体。:)

对于Arial,您需要在代码中设置字体。

我想我已经太迟了,但也许这个解决方案对其他人有帮助。要使用自定义字体,请将字体文件放在字体目录中。

textView.setTypeface(ResourcesCompat.getFont(this, R.font.lato));

在res/layout/value/style.xml中设置样式如下:

<style name="boldText">
    <item name="android:textStyle">bold|italic</item>
    <item name="android:textColor">#FFFFFF</item>
</style>

要在main.xml文件中使用此样式,请使用:

style="@style/boldText"

我使用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);

也许这有帮助。