自定義字體和XML布局(Android)我試圖在Android中使用XML文件定義GUI布局。據(jù)我所知,沒有辦法指定您的小部件應(yīng)該在XML文件中使用自定義字體(例如,您在資產(chǎn)/字體/中放置的字體),而且您只能使用已安裝的系統(tǒng)字體。我知道,在Java代碼中,我可以使用唯一的ID手動更改每個小部件的字體?;蛘?,我可以迭代Java中的所有小部件來進(jìn)行此更改,但這可能會非常慢。我還有別的選擇嗎?有什么更好的方法來制作具有自定義外觀的小部件嗎?我并不特別想手動更改我添加的每個新小部件的字體。
3 回答

largeQ
TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個贊
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;}}
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()); }}
<com.yourpackage.views.NativelyCustomTextView 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());
添加回答
舉報
0/150
提交
取消