如何獲取自定義TextView的屬性設置字體爲Textview的自定義TextView屬性。 基於屬性值的TextView從XML獲取自定義文本視圖的自定義屬性
public class MyTextView extends TextView
{
public MyTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public MyTextView(Context context)
{
super(context);
init();
}
public void init()
{
// set font_name based on attribute value of textview in xml file
String font_name = "";
if (!isInEditMode())
{
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/"+font_name);
setTypeface(tf);
}
}
設置字體在XML文件中
<com.Example.MyTextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fontname="font.ttf"
android:text="Header"
/>
我也把font.ttf文件中的資產 - >字體 謝謝