I always use 2 fonts, 1 normal and 1 bold. Therefore, when you need to boldly, just change your font. There has never been a problem, you can also create your own text screen.
Something like that:
/res/values/attrs.xml
<resources> <attr name="fontFamily" format="enum"> <enum name="helvetica" value="0"/> <enum name="helvetica_bold" value="1"/> </attr>
your TextViewPlus:
public class TextViewPlus extends TextView{ private static final String TAG = "TextViewPlus"; public TextViewPlus(Context context) { super(context); } public TextViewPlus(Context context, AttributeSet attrs) { super(context, attrs); setCustomFont(context, attrs); } public TextViewPlus(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setCustomFont(context, attrs); } private void setCustomFont(Context ctx, AttributeSet attrs) { TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus); int customFont = a.getInt(R.styleable.TextViewPlus_fontFamily, -1); setCustomFont(ctx, customFont); a.recycle(); } public boolean setCustomFont(Context ctx, int font) { Typeface tf = null; try { tf = Typefaces.get(ctx, font); } catch (Exception e) { Log.e(TAG, "Could not get typeface: "+e.getMessage()); return false; } setTypeface(tf); return true; } public boolean setCustomFont(Context ctx, String asset) { Typeface tf = null; try { tf = Typefaces.get(ctx, asset); } catch (Exception e) { Log.e(TAG, "Could not get typeface: "+e.getMessage()); return false; } setTypeface(tf); return true; }
}
How to use:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.myapp" android:layout_width="match_parent" android:layout_height="wrap_content" > <com.myapp.viewhelpers.TextViewPlus app:fontFamily="helvetica" android:layout_width="wrap_content" android:layout_height="wrap_content" app:fontFamily="helvetica" android:text="helo world"/>
Scoup source share