Arial font for text in Android

2020-07-04 06:52发布

问题:

I want to show text in Arial font. But Arial font is not available in android system fonts. I don't want to use arial ttf file in my application. Is there any other way to apply text with Arial font.

回答1:

If the font is not available in the android system, then you have to use the font file to apply that particular font say arial to your textView. May I know why you are not willing to use the font file to apply as it gives the same functionality.

Sample usage for using the font file is:

Typeface tfArial = Typeface.createFromAsset(getAssets(), "arial.ttf");
TextView tv = null;
// find the textview id from layout or create dynamically
tv.setTypeface(tfArial);

EDIT:

You need to put the font file arial.ttf in your asset folder.



回答2:

Download Arial font and in assets create folder name with "fonts" and save font at there

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


回答3:

Consider using Calligraphy to have any custom font in your Android application.

You will not need to add code in each TextView to apply a font. Simply initialise at application startup and you're good to go.



回答4:

Create a TextView from Java Code or XML like this

<?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"
/>

Make sure to keep the id as it is here because the TabLayout check for this ID if you use custom textview

Then from code inflate this layout and set the custom Typeface on that textview and add this custom view to the tab

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);

}


回答5:

You can create your custom font just adding the .ttf file in "fonts" folder inside "res":

The xml files contain this:

<?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>

And that's all! You can use this font in every TextView just adding android:fontFamily property.

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


标签: android fonts