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


当前回答

新字体资源允许使用

android:fontFamily="@font/my_font_in_font_folder"

其他回答

在某些情况下,这里有一种更简单的方法。其原理是在xml布局中添加不可见的TextVview,并在java代码中获取其typeFace。

xml文件中的布局:

<TextView
    android:text="The classic bread is made of flour hot and salty. The classic bread is made of flour hot and salty. The classic bread is made of flour hot and salty."
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:fontFamily="sans-serif-thin"
    android:id="@+id/textViewDescription"/>

java代码:

myText.setTypeface(textViewSelectedDescription.getTypeface());

它对我很有用(例如在TextSwitcher中)。

通过使用这个,可以动态地将fontfamily设置为类似于xml中的android:fontfamily,

For Custom font:

 TextView tv = ((TextView) v.findViewById(R.id.select_item_title));
 Typeface face=Typeface.createFromAsset(getAssets(),"fonts/mycustomfont.ttf"); 
 tv.setTypeface(face);

For Default font:

 tv.setTypeface(Typeface.create("sans-serif-medium",Typeface.NORMAL));

以下是使用的默认字体系列的列表,可以通过替换双引号字符串“sans-serif medium”来使用这些字体

FONT FAMILY                    TTF FILE                    

1  casual                      ComingSoon.ttf              
2  cursive                     DancingScript-Regular.ttf   
3  monospace                   DroidSansMono.ttf           
4  sans-serif                  Roboto-Regular.ttf          
5  sans-serif-black            Roboto-Black.ttf            
6  sans-serif-condensed        RobotoCondensed-Regular.ttf 
7  sans-serif-condensed-light  RobotoCondensed-Light.ttf   
8  sans-serif-light            Roboto-Light.ttf            
9  sans-serif-medium           Roboto-Medium.ttf           
10  sans-serif-smallcaps       CarroisGothicSC-Regular.ttf 
11  sans-serif-thin            Roboto-Thin.ttf             
12  serif                      NotoSerif-Regular.ttf       
13  serif-monospace            CutiveMono.ttf              

“mycustomfont.ttf”是ttf文件。路径将在src/assets/fonts/mycustomfont.ttf中,您可以参考此默认字体系列中有关默认字体的更多信息

试试看:

TextView textview = (TextView) findViewById(R.id.textview);
Typeface tf= Typeface.createFromAsset(getAssets(),"fonts/Tahoma.ttf");
textview .setTypeface(tf);

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

在XML中,您可以做的是

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

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

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

Kotlin代码-从资源文件夹设置自定义字体的文本视图

从res->font->avenir_ext_regular.ttf设置自定义字体

textView!!.typeface = ResourcesCompat.getFont(context!!, R.font.avenir_next_regular)