我应用了一个自定义字体TextView,但它似乎没有改变字体。
这是我的代码:
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(myTypeface);
谁能帮我摆脱这个问题?
我应用了一个自定义字体TextView,但它似乎没有改变字体。
这是我的代码:
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(myTypeface);
谁能帮我摆脱这个问题?
当前回答
Open your project and select Project on the top left app --> src --> main right click to main and create directory name it as assets right click to assest and create new directory name it fonts you need to find free fonts like free fonts give it to your Textview and call it in your Activity class copy your fonts inside the fonts folder TextView txt = (TextView) findViewById(R.id.txt_act_spalsh_welcome); Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Aramis Italic.ttf"); txt.setTypeface(font);
字体名称必须正确,有乐趣
其他回答
在API 26的官方文档中描述了这样做的正确方法:
https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html
这包括将ttf文件放在res/font文件夹中,并创建一个字体家族文件。
你可以在https://github.com/neopixl/PixlUI上使用PixlUI
导入他们的.jar并在XML中使用它
<com.neopixl.pixlui.components.textview.TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
pixlui:typeface="GearedSlab.ttf" />
Open your project and select Project on the top left app --> src --> main right click to main and create directory name it as assets right click to assest and create new directory name it fonts you need to find free fonts like free fonts give it to your Textview and call it in your Activity class copy your fonts inside the fonts folder TextView txt = (TextView) findViewById(R.id.txt_act_spalsh_welcome); Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Aramis Italic.ttf"); txt.setTypeface(font);
字体名称必须正确,有乐趣
最简单的解决方案android支持现在!
使用自定义字体在xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/[your font resource]"/>
看详细信息:
https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html
在Mobiletuts+上有一个关于Android文本格式的很好的教程。快速提示:自定义Android字体
编辑:现在我自己测试了一下。这是解决方案。你可以使用一个子文件夹称为字体,但它必须在资产文件夹,而不是在res文件夹。所以
资产/字体
还要确保字体结尾。我是说字体文件本身的结尾都是小写。换句话说,它不应该是myfont。ttf,而应该是myfont。ttf,这种方式必须小写