我将简单的小部件编写为具有某些属性的 TextView ,我想在扩展的 TextView 类中为其设置字体,如何执行此操作并且我可以拥有此功能?
属性:
<resources>
<declare-styleable name="TextViewStyle">
<attr name="selected_background" format="integer" />
<attr name="font" format="string" />
</declare-styleable>
</resources>
自定义 TextView 小部件:
public class TextViewStyle extends TextView{
public TextViewStyle(Context context) {
super(context);
}
public TextViewStyle(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TextViewStyle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextViewStyle, defStyle, 0);
a.recycle();
}
}
将简单的 UI 小部件转换为 xml:
<ir.jaziire.widgets.TextViewStyle
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/topic"
android:textColor="#000"
android:textSize="14dp"
android:gravity="right"
/>
在这个小部件中,我想设置 app:font=""
以设置 Assets 中的任何字体
最佳答案
自定义 TextView :
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;
import com.androidhub.R;
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
try {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.font, defStyle, 0);
String str = a.getString(R.styleable.font_fonttype);
a.recycle();
switch (Integer.parseInt(str)) {
case 0:
str = "fonts/Trebuchet_MS.ttf";
break;
default:
break;
}
setTypeface(FontManager.getInstance(getContext()).loadFont(str));
} catch (Exception e) {
e.printStackTrace();
}
}
public CustomTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
@SuppressWarnings("unused")
private void internalInit(Context context, AttributeSet attrs) {
}
字体管理器:
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.graphics.Typeface;
public class FontManager {
private Map<String, Typeface> fontCache = new HashMap<String, Typeface>();
private static FontManager instance = null;
private Context mContext;
private FontManager(Context mContext2) {
mContext = mContext2;
}
public synchronized static FontManager getInstance(Context mContext) {
if (instance == null) {
instance = new FontManager(mContext);
}
return instance;
}
public Typeface loadFont(String font) {
if (false == fontCache.containsKey(font)) {
fontCache.put(font,
Typeface.createFromAsset(mContext.getAssets(), font));
}
return fontCache.get(font);
}
}
在 attrs.xml
文件中:
<declare-styleable name="font">
<attr name="fonttype">
<enum name="trebuchet_ms" value="0" />
</attr>
</declare-styleable>
在您的 xml
中使用您的 CustomTextView
作为:
在您的 xml
顶部声明它
xmlns:custom="http://schemas.android.com/apk/res/com.androidhub"
并且您可以将 CustomTextView
用作:
<com.utils.CustomTextView
android:id="@+id/loadMap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/custom_button_selector"
android:clickable="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="center"
android:marqueeRepeatLimit="marquee_forever"
android:padding="10dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="@string/load_map"
android:textColor="@color/home_buttons_selector"
android:textSize="16sp"
custom:fonttype="trebuchet_ms" />
确保您将字体放在 assests-->fonts-->YourFont
com.androidhub
是我的包名。
关于Android 为自定义 TextView 小部件设置字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28362023/