我应用了一个自定义字体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);
谁能帮我摆脱这个问题?
当前回答
不幸的是,这个问题没有好的解决方案。
我已经看到了许多关于使用自定义TextView的文章,但他们忘记了,这不仅是textviews,可以实现字体&有textviews隐藏在其他视图无法访问的开发人员;我还没开始讲Spannable呢。
你可以使用外部字体工具,比如:
书法字体工具
但是这在应用程序的创建上循环每个视图,甚至这个实用程序错过了一些视图(ViewPager呈现正常字体),然后你有问题,当谷歌更新他们的构建工具,这偶尔会崩溃,因为它需要针对已弃用的属性。由于使用Java的反射,它也有点慢。
这真的是由谷歌来解决的。Android需要更好的字体支持。如果你看看iOS的解决方案,他们实际上有100种字体可供选择。想要自定义字体?简单地丢一个TFF,它是可用的..
目前,我们目前仅限于谷歌提供的服务,这是非常有限的,但幸运的是,移动优化。
其他回答
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);
字体名称必须正确,有乐趣
你可以在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" />
我也遇到了同样的问题,TTF没有出现。我改变了字体文件,与相同的代码,它的工作。
是的,就像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
随着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>