我应用了一个自定义字体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);
谁能帮我摆脱这个问题?
当前回答
在API 26的官方文档中描述了这样做的正确方法:
https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html
这包括将ttf文件放在res/font文件夹中,并创建一个字体家族文件。
其他回答
您可以使用简单的EasyFonts第三方库来设置各种自定义字体到您的TextView。通过使用这个库,你应该不必担心下载和添加字体到assets/fonts文件夹。还有关于字体对象的创建。
而不是
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(myTypeface);
简单:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
这个库还提供了以下字体面。
Roboto Droid衬线 机器人机器人 自由 有趣的栽培者 Android的国家 绿色的鳄梨 识别
我也遇到了同样的问题,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
在API 26的官方文档中描述了这样做的正确方法:
https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html
这包括将ttf文件放在res/font文件夹中,并创建一个字体家族文件。