我应用了一个自定义字体TextView,但它似乎没有改变字体。

这是我的代码:

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
    TextView myTextView = (TextView)findViewById(R.id.myTextView);
    myTextView.setTypeface(myTypeface);

谁能帮我摆脱这个问题?


当前回答

你可以在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);

字体名称必须正确,有乐趣

在API 26的官方文档中描述了这样做的正确方法:

https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html

这包括将ttf文件放在res/font文件夹中,并创建一个字体家族文件。

是的,就像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

您可以使用简单的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的国家 绿色的鳄梨 识别

如果你想从网络加载字体或轻松地设置它的样式,你可以使用:

https://github.com/shellum/fontView

例子:

<!--Layout-->
<com.finalhack.fontview.FontView
        android:id="@+id/someFontIcon"
        android:layout_width="80dp"
        android:layout_height="80dp" />

//Java:
fontView.setupFont("http://blah.com/myfont.ttf", true, character, FontView.ImageType.CIRCLE);
fontView.addForegroundColor(Color.RED);
fontView.addBackgroundColor(Color.WHITE);