gpt4 book ai didi

android - 在 Android Studio XML 的预览部分显示自定义字体或 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:42:52 26 4
gpt4 key购买 nike

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

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

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

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

enter image description here

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

enter image description here

最佳答案

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

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

Here is full demo code in gist

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

all code in 1 picture

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

TextViewWithFont.java - 自定义 View 类

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 - Layout 以及如何使用 TextViewWithFont 自定义 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>

关于android - 在 Android Studio XML 的预览部分显示自定义字体或 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34613628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com