Moh*_*man 4 android android-custom-view android-layout font-awesome
有没有办法在Android Studio的预览部分查看自定义字体/视图?
我使用font-awesome作为自定义字体在我的应用程序中显示麦克风图标.一切都很好.但是众所周知,预览部分无法加载自定义视图.
在编码时是否有任何插件或黑客可以在预览窗口中查看自定义视图?
这就是我在我的应用上加载的内容:
这是我在预览部分看到的:
var*_*ren 10
要在Android Studio XML设计器中显示FontAwesome图标,您可以.
使用评论代码演示img:
重要部分:(几乎与使用XML声明自定义Android UI元素但调整较小相同)
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
res/values/attrs.xml - 需要app:customFont="fontawesome-webfont.ttf"在我们的布局xml中使用它.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextViewPlusFont">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
只要正确编写这些视图,预览部分就可以很好地加载自定义视图。您必须记住所有小细节,例如绘制/onDraw/dispatchDraw方法、测量和布局、设置正确的主题、样式、提供editMode数据等。
问题是 Android Studio 有自己的 Context 和 Resources 类,它们无法执行某些操作。例如,这些类缺乏从资产文件夹读取资产和从原始文件夹读取原始资源的实现。
要加载自定义字体,您需要在 Android Studio 中无权访问的资源文件夹。自定义视图或多或少应该可以工作。
| 归档时间: |
|
| 查看次数: |
6495 次 |
| 最近记录: |