Custom fonts and XML layouts (Android)

后端 未结 18 1875
执念已碎
执念已碎 2020-11-22 07:21

I\'m trying to define a GUI layout using XML files in Android. As far as I can find out, there is no way to specify that your widgets should use a custom font (e.g. one you\

相关标签:
18条回答
  • 2020-11-22 07:40

    The best way to do it From Android O preview release is this way
    1.)Right-click the res folder and go to New > Android resource directory. The New
    Resource Directory window appears.
    2.)In the Resource type list, select font, and then click OK.
    3.)Add your font files in the font folder.The folder structure below generates R.font.dancing_script, R.font.la_la, and R.font.ba_ba.
    4.)Double-click a font file to preview the file's fonts in the editor.

    Next we must create a font family

    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 tag 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/hey_regular" />
        <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/hey_bababa" />
    </font-family>
    

    Adding fonts to a TextView:

       <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        **android:fontFamily="@font/ba_ba"**/>
    

    As from the documentation

    Working With Fonts

    all the steps are correct.

    0 讨论(0)
  • 2020-11-22 07:40

    Extend TextView and give it a custom attribute or just use the android:tag attribute to pass in a String of what font you want to use. You will need to pick a convention and stick to it such as I will put all of my fonts in the res/assets/fonts/ folder so your TextView class knows where to find them. Then in your constructor you just set the font manually after the super call.

    0 讨论(0)
  • 2020-11-22 07:41

    You can't extend TextView to create a widget or use one in a widgets layout: http://developer.android.com/guide/topics/appwidgets/index.html

    0 讨论(0)
  • 2020-11-22 07:44

    If you only have one typeface you would like to add, and want less code to write, you can create a dedicated TextView for your specific font. See code below.

    package com.yourpackage;
    import android.content.Context;
    import android.graphics.Typeface;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    public class FontTextView extends TextView {
        public static Typeface FONT_NAME;
    
    
        public FontTextView(Context context) {
            super(context);
            if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/FontName.otf");
            this.setTypeface(FONT_NAME);
        }
        public FontTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/FontName.otf");
            this.setTypeface(FONT_NAME);
        }
        public FontTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/FontName.otf");
            this.setTypeface(FONT_NAME);
        }
    }
    

    In main.xml, you can now add your textView like this:

    <com.yourpackage.FontTextView
        android:id="@+id/tvTimer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />
    
    0 讨论(0)
  • 2020-11-22 07:44

    The only way to use custom fonts is through the source code.

    Just remember that Android runs on devices with very limited resources and fonts might require a good amount of RAM. The built-in Droid fonts are specially made and, if you note, have many characters and decorations missing.

    0 讨论(0)
  • 2020-11-22 07:48

    Here's a tutorial that shows you how to setup a custom font like @peter described: http://responsiveandroid.com/2012/03/15/custom-fonts-in-android-widgets.html

    it also has consideration for potential memory leaks ala http://code.google.com/p/android/issues/detail?id=9904 . Also in the tutorial is an example for setting a custom font on a button.

    0 讨论(0)
提交回复
热议问题