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

在Android Studio XML的预览部分显示自定义字体或视图,android,android-layout,android-custom-view,font-awesome,Android,Android Layout,Android Custom View,Font Awesome,有没有办法在Android Studio的预览部分查看自定义字体/视图? 我使用了font-awesome作为自定义字体,在我的应用程序中显示麦克风图标。一切正常。但众所周知,预览部分无法加载自定义视图 是否有任何插件或黑客可以在编码时在预览窗口中查看自定义视图 这是我在应用程序上加载的内容: 这是我在预览部分看到的内容: 预览部分可以很好地加载自定义视图,只要这些视图编写正确。您必须记住所有小细节,如draw/onDraw/dispatchDraw方法、测量和布局、设置正确的主题、样式、提供

有没有办法在Android Studio的预览部分查看自定义字体/视图?

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

是否有任何插件或黑客可以在编码时在预览窗口中查看自定义视图

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

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


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

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


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

要在Android Studio XML designer中显示图标,您可以

  • 创建自定义视图
  • 在构造函数中应用字体
  • 如果要从xml设置字体,请添加自定义属性
  • 使用注释中的代码演示img:

    重要部分:(与基本相同,但微调较小)

    TextViewWithFont.java-自定义视图类

    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=“fontsawesome webfont.ttf”
    需要这个

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="TextViewPlusFont">
            <attr name="customFont" format="string"/>
        </declare-styleable>
    </resources>
    
    
    
    Typefaces.java-用于重用字体的助手类(字体缓存)

    导入android.content.Context;
    导入android.graphics.Typeface;
    导入android.util.Log;
    导入java.util.Hashtable;
    公共类字体{
    私有静态最终字符串TAG=“Typefaces”;
    私有静态最终哈希表缓存=新哈希表();
    公共静态字体get(上下文c,字符串assetPath){
    已同步(缓存){
    如果(!cache.containsKey(assetPath)){
    试一试{
    Typeface t=Typeface.createFromAsset(c.getAssets(),
    资产路径);
    cache.put(assetPath,t);
    Log.e(标签“已加载””+资产路径);
    }捕获(例外e){
    Log.e(标记“无法获取字体””+assetPath
    +“'因为”+e.getMessage());
    返回null;
    }
    }
    返回cache.get(assetPath);
    }
    }
    }
    
    活动\u main.xml-布局和如何使用文本视图和字体自定义视图

    <?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>
    
    
    
    如果你的意思是在开发环境中,比如Android Studio,那么简单的答案是否定的。我从未见过或听说过这样的工具、黑客或变通方法。尽管如此,我还是很想看看是否有其他人发现了这一点,因为多年来这一直是开发人员的问题。我正在寻找一种在开发环境(Android Studio)中解决的方法。@MohammadArman我认为这取决于您的实现。在我的Android Studio中,我可以看到令人敬畏的图标。这里的代码非常简单。1) 创建自定义视图2)在构造函数中应用字体3)添加自定义属性如果你想从xml@varren设置字体,你的演示img和示例代码帮助我解决了这个问题。你能把你的评论作为回答吗。我将把它标记为已接受的答案。:)@MohammadArman很高兴听到这有帮助。
    <?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>