【问题标题】:Showing custom font or View in preview section of Android Studio XML在 Android Studio XML 的预览部分显示自定义字体或视图
【发布时间】:2016-01-05 14:08:54
【问题描述】:

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

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

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

这是我在应用中加载的内容:

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

【问题讨论】:

  • 如果你的意思是在开发环境中,比如 Android Studio,简短的回答是否定的。我从未见过或听说过这样的工具、hack 或变通方法。尽管如此,我还是很想看看是否有其他人认为这是多年来一直困扰开发者的问题
  • 是的,我正在寻找开发环境 (Android Studio) 中的工作。
  • @MohammadArman 我认为这取决于您的实施。在我的 Android Studio 中,我可以看到 FontAwesome 图标。这是demo img 代码非常简单。 1) 创建自定义视图 2) 在构造函数中应用字体 3) 如果要从 xml CODE 设置字体,请添加自定义 attr
  • @varren 您的演示 img 和示例代码帮助我解决了这个问题。您能否发表您的评论作为答案。我会将其标记为已接受的答案。 :)
  • @MohammadArman 很高兴听到它有帮助。

标签: android android-layout android-custom-view font-awesome


【解决方案1】:

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

  1. 创建自定义视图
  2. 在构造函数中应用字体
  3. 如果要从 xml 设置字体,请添加自定义 attr

Here is full demo code in gist

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

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

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="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 - 重用字体的辅助类(字体缓存)

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>

【讨论】:

    【解决方案2】:

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

    问题在于 Android Studio 有自己的 Context 和 Resources 类,它们无法执行某些操作。例如,这些类缺乏从 assets 文件夹中读取资产和从 raw 文件夹中读取原始资源的实现。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-26
      • 2019-09-28
      • 2023-02-17
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多