您好我正在学习如何为Android应用程序创建库。我从自定义textview
开始,并在其上设置自定义字体。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lobster"/>
我希望以fontFamily
格式获取@font/font_name
值。开发人员需要在res
文件夹中创建字体文件夹并将字体放在那里。我需要做些什么?提前谢谢。
我使用以下方法创建了自定义属性。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Typekitable">
<attr name="fontFamily" format="string" />
</declare-styleable>
</resources>
答案 0 :(得分:0)
您可以使用:
1-创建这样的枚举:
<attr name="fontFamily" format="enum" >
<enum name="tahoma" value="1"/>
</attr>
2 -
<declare-styleable name="MyTextView">
<attr name="fontFamily"/>
</declare-styleable>
3 -
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.MyTextView, defStyle, 0);
int fontOrdinal = a.getInt(R.styleable.MyTextView_fontFamily, 0);
//....
a.recycle();
}
}
使用时:
<MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="tahoma"/> // you can use ctrl+space
默认情况下,您无法在Res中创建文件夹自定义。为此,请参阅以下链接:
With two res directories per build variant, Gradle stopped tracking changes in resource files