Arial font for text in Android

后端 未结 6 1503
悲哀的现实
悲哀的现实 2021-02-20 04:11

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 appl

相关标签:
6条回答
  • 2021-02-20 04:39

    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.

    0 讨论(0)
  • 2021-02-20 04:39

    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.

    0 讨论(0)
  • 2021-02-20 04:48

    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);
    
    }
    
    0 讨论(0)
  • 2021-02-20 04:51

    You can add google font family from android studio by going to

    1. xml design
    2. add font family then there is limitem font family so click on
    3. more font family ( this is google font you can choose either add font or downloadable font)
    4. add font then click ok then the font will be added in your font list and you can use like default font.
    0 讨论(0)
  • 2021-02-20 04:52

    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);
    
    0 讨论(0)
  • 2021-02-20 05:00

    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"        />
    
    0 讨论(0)
提交回复
热议问题