简体   繁体   English

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

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

Is there any way to view custom fonts/views in the preview section of Android Studio? 有没有办法在Android Studio的预览部分查看自定义字体/视图?

I have used font-awesome as a custom typeface to show microphone icon in my app. 我使用font-awesome作为自定义字体在我的应用程序中显示麦克风图标。 Everything is working fine. 一切都很好。 But as we all know the preview section cannot load custom views. 但是众所周知,预览部分无法加载自定义视图。

Is there any plugin or hack to see the custom views in the preview window while coding? 在编码时是否有任何插件或黑客可以在预览窗口中查看自定义视图?

This is what I am loading on my app: 这就是我在我的应用上加载的内容:

在此输入图像描述

This is what I see in the preview section: 这是我在预览部分看到的:

在此输入图像描述

To make FontAwesome icons visible in Android Studio XML designer you can. 要在Android Studio XML设计器中显示FontAwesome图标,您可以。

  1. Create custom view 创建自定义视图
  2. Apply font in constructor 在构造函数中应用字体
  3. Add custom attr if you want to set font from xml 如果要从xml设置字体,请添加自定义attr

Here is full demo code in gist 这是完整的演示代码

Demo img with code from comment: 使用评论代码演示img:

1张图片中的所有代码

Important parts: (pretty much the same as Declaring a custom android UI element using XML but with small tuning) 重要部分:(几乎与使用XML声明自定义Android UI元素但调整较小相同)

TextViewWithFont.java - Custom view class 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 - Need this to use app:customFont="fontawesome-webfont.ttf" in our layout xml. 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 - Helper class to reuse fonts (Cache for fonts) Typefaces.java - 重用字体的Helper类(字体缓存)

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

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. 您必须记住所有小细节,如draw / onDraw / dispatchDraw方法,测量和布局,设置正确的主题,样式,提供editMode数据等。

The deal is that Android Studio has its own Context and Resources classes which are unable to perform certain things. 这笔交易是Android Studio有自己的Context和Resources类,无法执行某些操作。 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. 要加载自定义字体,您需要在Android Studio中无权访问的资源文件夹。 Custom view should work, more or less. 自定义视图应该或多或少地起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM