xml android 字体,自定义字体和XML布局(Android)
這可能有點(diǎn)晚,但您需要?jiǎng)?chuàng)建一個(gè)返回自定義字體的單例類,以避免內(nèi)存泄漏。
字體類:public?class?OpenSans?{private?static?OpenSans?instance;private?static?Typeface?typeface;public?static?OpenSans?getInstance(Context?context)?{
synchronized?(OpenSans.class)?{
if?(instance?==?null)?{
instance?=?new?OpenSans();
typeface?=?Typeface.createFromAsset(context.getResources().getAssets(),?"open_sans.ttf");
}
return?instance;
}}public?Typeface?getTypeFace()?{
return?typeface;}}
自定義TextView:public?class?NativelyCustomTextView?extends?TextView?{
public?NativelyCustomTextView(Context?context)?{
super(context);
setTypeface(OpenSans.getInstance(context).getTypeFace());
}
public?NativelyCustomTextView(Context?context,?AttributeSet?attrs)?{
super(context,?attrs);
setTypeface(OpenSans.getInstance(context).getTypeFace());
}
public?NativelyCustomTextView(Context?context,?AttributeSet?attrs,
int?defStyle)?{
super(context,?attrs,?defStyle);
setTypeface(OpenSans.getInstance(context).getTypeFace());
}}
通過XML:
android:id="@+id/natively_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:text="@string/natively"
android:textSize="30sp"?/>
以編程方式:TextView?programmaticallyTextView?=?(TextView)
findViewById(R.id.programmatically_text_view);programmaticallyTextView.setTypeface(OpenSans.getInstance(this)
.getTypeFace());
總結(jié)
以上是生活随笔為你收集整理的xml android 字体,自定义字体和XML布局(Android)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言结构体的位操作,C语言之路---结
- 下一篇: android 绘制5格电量,Andro