我应用了一个自定义字体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);
谁能帮我摆脱这个问题?
当前回答
确保在调用super和setContentView()之后将上述代码粘贴到onCreate()中。这个小细节让我挂念了一会儿。
其他回答
我以前成功地使用过这个方法。我们的实现之间的唯一区别是,我没有在资产中使用子文件夹。但不确定这是否会改变什么。
在API 26的官方文档中描述了这样做的正确方法:
https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html
这包括将ttf文件放在res/font文件夹中,并创建一个字体家族文件。
我也遇到了同样的问题,TTF没有出现。我改变了字体文件,与相同的代码,它的工作。
随着Android 8.0在应用程序中使用自定义字体变得容易下载字体。 我们可以直接将字体添加到项目文件夹中的res/font/文件夹中,这样一来,字体就会自动在Android Studio中可用。
现在将fontFamily属性设置为字体列表或单击更多并选择您选择的字体。这将添加tools:fontFamily="@font/your_font_file"行到你的TextView。
这将自动生成几个文件。
1. 在values文件夹中,它将创建fonts_certs.xml。
2. 在Manifest中,它会添加这样的行:
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
3. preloaded_fonts.xml
<resources>
<array name="preloaded_fonts" translatable="false">
<item>@font/open_sans_regular</item>
<item>@font/open_sans_semibold</item>
</array>
</resources>
是的,就像Dipali说的那样,下载字体非常简单。
你是这样做的……
Place a TextView. In the properties pane, select the fontFamily dropdown. If it isn't there, find the caret thingy (the > and click on it to expand textAppearance) under the. Expand the font-family drop down. In the little list, scroll all the way down till you see more fonts This will open up a dialog box where you can search from Google Fonts Search for the font you like with the search bar at the top Select your font. Select the style of the font you like (i.e. bold, normal, italic, etc) In the right pane, choose the radio button that says Add font to project Click okay. Now your TextView has the font you like!
奖金: 如果你想在你的应用程序中使用所选字体的文本样式,只需添加<item name="android:fontfamily">@font/fontnamehere</item>到你的styles.xml