Set font at runtime, TextView

前端 未结 9 666
温柔的废话
温柔的废话 2020-11-29 22:00

How to set the font of a TextView created at runtime?

I created a TextView

Textview tv = new TextView(this);      
tv.setTextSize(20);

相关标签:
9条回答
  • 2020-11-29 22:13

    You can use the following code to set all your text to a specific font at runtime. Just call the setViewGroupFont method at the end of your Activity onCreate method or whenever you dynamically create new views:

    private static final String FONT_NAME = "fonts/Roboto-Regular.ttf";
    private static Typeface m_font = null;
    
    public static Typeface getFont(Context p_context)
    {
        if (null == m_font && null != p_context)
        {
            m_font = Typeface.createFromAsset(p_context.getApplicationContext().getAssets(), FONT_NAME);
        }
        return m_font;
    }
    
    public static void setViewGroupFont(ViewGroup p_viewGroup)
    {
        if (null != p_viewGroup)
        {
            for (int currChildIndex = 0; currChildIndex < p_viewGroup.getChildCount(); currChildIndex++)
            {
                View currChildView = p_viewGroup.getChildAt(currChildIndex);
    
                if (ViewGroup.class.isInstance(currChildView))
                {
                    setViewGroupFont((ViewGroup) currChildView);
                }
                else
                {
                    try
                    {
                        Method setTypefaceMethod = currChildView.getClass().getMethod("setTypeface", Typeface.class);
    
                        setTypefaceMethod.invoke(currChildView, getFont(p_viewGroup.getContext()));
                    }
                    catch (NoSuchMethodException ex)
                    {
                        // Do nothing
                    }
                    catch (Exception ex)
                    {
                        // Unexpected error setting font
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 22:17

    With introduction of Fonts in XML in Android 8.0 (backward compatible from API version 14) its very easy to set font from xml itself.

    From the android documentation:

    Android 8.0 (API level 26) introduces a new feature, Fonts in XML, which lets you use fonts as resources. You can add the font file in the res/font/ folder to bundle fonts as resources. These fonts are compiled in your R file and are automatically available in Android Studio. You can access the font resources with the help of a new resource type, font. For example, to access a font resource, use @font/myfont, or R.font.myfont.

    Firstly create a Android Resource Directory in res folder named as font
    Add your .ttf font file to that directory, and then create font family

    Create a font family

    A font family is a set of font files along with its style and weight details. In Android, you can create a new font family as an XML resource and access it as a single unit, instead of referencing each style and weight as separate resources. By doing this, the system can select the correct font based on the text style you are trying to use.

    To create a font family, perform the following steps in the Android Studio:

    1. Right-click the font folder and go to New > Font resource file. The New Resource File window appears.
    2. Enter the file name, and then click OK. The new font resource XML opens in the editor.
    3. Enclose each font file, style, and weight attribute in the <font> element. The following XML illustrates adding font-related attributes in the font resource XML:

      <?xml version="1.0" encoding="utf-8"?> <font-family xmlns:android="http://schemas.android.com/apk/res/android"> <font android:fontStyle="normal" android:fontWeight="400" android:font="@font/lobster_regular" /> <font android:fontStyle="italic" android:fontWeight="400" android:font="@font/lobster_italic" /> </font-family>

    Then use the following code to set font in your textView like

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/lobster"/>
    
    0 讨论(0)
  • 2020-11-29 22:27

    To set In-built Font at Run-Time:

    • First of all, To Change Font-face, a Typeface class is used.

    • Now, at Run-Time, to set the font-face, Use setTypeface(Typeface) from the Java code

    • at Design-Time, to set the font-face, Use android:typeface="serif"

    For example:

    <TextView android:text="@+id/TextView01"
     android:id="@+id/TextView01"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textSize="30px"
     android:textStyle="italic"
     android:typeface="serif" />
    

    To set Custom font(s) in your Android application

    To do this, simply create an assets/ folder in the project root, and put your fonts (in TrueType, or TTF, form) in the assets. You might, for example, create assets/fonts/ and put your TTF files in there:

      TextView tv=(TextView)findViewById(R.id.custom); 
      Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf"); 
      tv.setTypeface(face); 
    
    0 讨论(0)
提交回复
热议问题