Showing custom font or View in preview section of Android Studio XML

后端 未结 2 1538
陌清茗
陌清茗 2021-02-09 15:30

Is there any way to view custom fonts/views in the preview section of Android Studio?

I have used font-awesome as a custom typeface

相关标签:
2条回答
  • 2021-02-09 15:43

    To make FontAwesome icons visible in Android Studio XML designer you can.

    1. Create custom view
    2. Apply font in constructor
    3. Add custom attr if you want to set font from xml

    Here is full demo code in gist

    Demo img with code from comment:

    Important parts: (pretty much the same as Declaring a custom android UI element using XML but with small tuning)

    TextViewWithFont.java - Custom view class

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    public class TextViewWithFont extends TextView {
    
        public TextViewWithFont(Context context) {
            super(context);
            init(context, null, 0);
        }
        public TextViewWithFont(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context, attrs, 0);
        }
        public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(context, attrs, defStyle);
        }
        private void init(Context context, AttributeSet attrs, int defStyle) {
            // Load attributes
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TextViewPlusFont, 0, 0);
            try {
                String fontInAssets = ta.getString(R.styleable.TextViewPlusFont_customFont);
                setTypeface(Typefaces.get(context, "fonts/"+ fontInAssets));
            } finally {
                ta.recycle();
            }
        }
    }
    

    res/values/attrs.xml - Need this to use app:customFont="fontawesome-webfont.ttf"in our layout xml.

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="TextViewPlusFont">
            <attr name="customFont" format="string"/>
        </declare-styleable>
    </resources>
    

    Typefaces.java - Helper class to reuse fonts (Cache for fonts)

    import android.content.Context;
    import android.graphics.Typeface;
    import android.util.Log;
    import java.util.Hashtable;
    
    public class Typefaces {
        private static final String TAG = "Typefaces";
    
        private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
    
        public static Typeface get(Context c, String assetPath) {
            synchronized (cache) {
                if (!cache.containsKey(assetPath)) {
                    try {
                        Typeface t = Typeface.createFromAsset(c.getAssets(),
                                assetPath);
                        cache.put(assetPath, t);
                        Log.e(TAG, "Loaded '" + assetPath);
                    } catch (Exception e) {
                        Log.e(TAG, "Could not get typeface '" + assetPath
                                + "' because " + e.getMessage());
                        return null;
                    }
                }
                return cache.get(assetPath);
            }
        }
    }
    

    activity_main.xml - Layout and how to use TextViewWithFont custom view

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    
        <com.example.TextViewWithFont
            xmlns:app="http://schemas.android.com/apk/res/com.example"
            app:customFont="fontawesome-webfont.ttf"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="\uf1e2"
            android:textSize="60dp"/>
    </LinearLayout>
    
    0 讨论(0)
  • 2021-02-09 16:04

    Preview section loads custom views fine as long as these views are correctly written. You have to remember about all small details like draw/onDraw/dispatchDraw methods, measuring and layouting, setting correct theme, styling, providing editMode data, etc.

    The deal is that Android Studio has its own Context and Resources classes which are unable to perform certain things. For example these classes lack implementation of reading assets from assets folder and raw resources from raw folder.

    To load a custom font, you need assets folder which you don't have access to in Android Studio. Custom view should work, more or less.

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