在 Android Studio XML 的预览部分显示自定义字体或视图

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

有什么方法可以在 Android Studio 的预览部分查看自定义 fonts/views 吗?

我使用 font-awesome 作为 自定义字体 在我的应用程序中显示麦克风图标。一切正常。但众所周知,预览部分无法加载自定义视图。

是否有任何插件或 hack 可以在编码时在预览 window 中查看自定义视图?

这是我在我的应用程序上加载的内容:

这是我在预览部分看到的:

只要正确编写这些视图,预览部分就可以正常加载自定义视图。您必须记住所有小细节,例如 draw/onDraw/dispatchDraw 方法、测量和布局、设置正确的主题、样式、提供 editMode 数据等。

交易是 Android Studio 有自己的上下文和资源 类,它们无法执行某些事情。例如,这些 类 缺乏从资产文件夹中读取资产和从原始文件夹中读取原始资源的实现。

要加载自定义字体,您需要在 Android Studio 中无法访问的资产文件夹。自定义视图或多或少应该可以工作。

要使 FontAwesome 图标在 Android Studio XML 设计器中可见,您可以。

  1. 创建自定义视图
  2. 在构造函数中应用字体
  3. 如果你想设置来自 xml
  4. 的字体,请添加自定义属性

Here is full demo code in gist

使用评论中的代码演示 img:

重要部分:(与Declaring a custom android UI element using XML几乎相同,但有小调整)

TextViewWithFont.java - 自定义视图 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 - 需要这个才能在我们的布局 xml.

中使用 app:customFont="fontawesome-webfont.ttf"
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextViewPlusFont">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources>

Typefaces.java - 帮助程序 class 重用字体(字体缓存)

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 - 布局以及如何使用 TextViewWithFont 自定义视图

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