Android中文本的Arial字体

11

我想展示Arial字体的文本。但是Android系统字体中没有Arial字体。我不想在我的应用程序中使用arial ttf文件。还有其他方法来应用Arial字体吗?


4
不行,任何不可用的字体都需要.ttf / .otf文件。 - Siddharth Lele
5
并非所有字体都可以用于商业用途,因此在使用之前务必先检查。话虽如此,我通常会前往http://www.fontsquirrel.com/下载免费字体库,宁可安全也不要后悔。至于Arial字体的授权许可,请查看维基百科上的此链接:http://en.wikipedia.org/wiki/Arial#Free_alternatives - Siddharth Lele
不行,你必须使用ttf格式的Arial字体。 - Abhijit Chakra
如何使用自定义字体?能否提供一些示例? - jkstar
http://vimaltuts.com/android-tutorial-for-beginners/android-custom-font-example - QuokMoon
显示剩余4条评论
6个回答

10
如果Android系统中没有可用的字体,则必须使用字体文件来应用特定的字体,比如Arial,到您的textView中。请问为什么您不愿意使用字体文件来应用这个字体呢?因为这样做可以提供相同的功能。
使用字体文件的示例用法为:
Typeface tfArial = Typeface.createFromAsset(getAssets(), "arial.ttf");
TextView tv = null;
// find the textview id from layout or create dynamically
tv.setTypeface(tfArial);

编辑:

您需要将字体文件arial.ttf放在您的asset文件夹中。


你的应用程序必须使用Arial字体。 - AndroidEnthusiastic

4

下载Arial字体,并在资源中创建名为“fonts”的文件夹,并将字体保存在其中。

Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/arial.ttf");
TextView tv = (TextView) findViewById(R.id.CustomFontText);
tv.setTypeface(tf);

1
你可以通过将.ttf文件添加到“res”文件夹中的“fonts”文件夹来创建自定义字体:

enter image description here

XML文件包含以下内容:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:font="@font/nunito_extrabold"
        android:fontStyle="normal"
        android:fontWeight="400" />
</font-family>

就是这样!您可以在每个TextView中添加android:fontFamily属性来使用此字体。

  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/font_semibold"
        android:text="@string/custom_font"        />

2
这仅适用于API 26。 - Yaroslav Rybalka

1
你可以通过以下步骤在Android Studio中添加谷歌字体族:
  1. 进入XML设计
  2. 添加字体族,然后会出现有限的字体族,点击“更多字体族”
  3. 选择Google字体族,你可以选择添加字体或可下载字体
  4. 添加字体,然后点击确定,该字体将被添加到字体列表中,你可以像使用默认字体一样使用它。

0
考虑在您的Android应用程序中使用Calligraphy来使用任何自定义字体。
您将不需要在每个TextView中添加代码来应用字体。只需在应用程序启动时进行初始化即可。

0
可以通过Java代码或XML创建一个TextView,像这样:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:textSize="15sp"
android:textColor="@color/tabs_default_color"
android:gravity="center"
android:layout_height="match_parent"
/>

请确保将ID保持不变,因为如果您使用自定义textview,则TabLayout会检查此ID

然后从代码中填充此布局并在该textview上设置自定义Typeface,并将此自定义视图添加到选项卡中

for (int i = 0; i < tabLayout.getTabCount(); i++) {
    //noinspection ConstantConditions
 TextView tv=(TextView)LayoutInflater.from(this).inflate(R.layout.custom_tab,null)
 tv.setTypeface(Typeface);       
 tabLayout.getTabAt(i).setCustomView(tv);

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接