Silakan ikuti kustomisasi dasar FontTextView, FontEditView, FontRadioButton, FontCheckBox dan FontButton.
[Untuk jawaban yang tepat, setelah melihat panduan ini, silakan lihat:
https://stackoverflow.com/a/51113022/787399 ]
Gunakan FontTextView kustom, dalam tata letak item ArrayAdapter, seperti ini:
public class FontEditText extends AppCompatEditText {
// private String FONT = "fonts/roboto_regular.ttf";
public FontEditText(Context context) {
super(context, null);
// setFontFromAsset(context, null, R.style.DefaultFontTextView);
// FONT = getContext().getString(R.string.font_roboto_regular);
}
public FontEditText(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setFontFromAsset(context, attrs, R.attr.fetFontStyle);
}
public FontEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setFontFromAsset(context, attrs, defStyleAttr);
}
private void setFontFromAsset(Context context, AttributeSet attrs, int defStyle) {
BaseActivity activity = (BaseActivity)((MyApplication) context.getApplicationContext()).getCurrentActivity();
FontAndLocaleManager fontAndLocaleManager = activity.getFontAndLocaleManager();
fontAndLocaleManager.setFontFromAsset(this, R.styleable.FontEditText, R.styleable.FontEditText_fetFontFace, attrs, defStyle);
}
}
gunakan kode:
public void setFontFromAsset(View view, int[] resViewStyleable, int resStyleableViewFontFace, AttributeSet attrs, int defStyle) {
String strFont = null;
Typeface tfFontFace = null;
String strButton = FontButton.class.getCanonicalName(),
strTextView = FontTextView.class.getCanonicalName(),
strEditText = FontEditText.class.getCanonicalName(),
strView = view.getClass().getCanonicalName();
try {
if (view.isInEditMode()) {
return;
}
//R.string.font_roboto_regular
strFont = context.getString(R.string.font_roboto_regular);
tfFontFace = Typeface.createFromAsset(context.getAssets(), strFont);
//AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes
//R.styleable.FontButton
TypedArray a = context.obtainStyledAttributes(attrs, resViewStyleable, defStyle, 0);
//R.styleable.FontButton_btFontFace
String derivedFont = a.getString(resStyleableViewFontFace);
a.recycle();
//==
try {
if (derivedFont != null) {
Typeface derivedFontFace = Typeface.createFromAsset(context.getAssets(), derivedFont);
if (strView.equals(strButton)) {
((FontButton) view).setTypeface(derivedFontFace);
} else if (strView.equals(strTextView)) {
((FontTextView) view).setTypeface(derivedFontFace);
} else if (strView.equals(strEditText)) {
((FontEditText) view).setTypeface(derivedFontFace);
}
return;
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (strFont != null && tfFontFace != null) {
if (strView.equals(strButton)) {
((FontButton) view).setTypeface(tfFontFace);
} else if (strView.equals(strTextView)) {
((FontTextView) view).setTypeface(tfFontFace);
} else if (strView.equals(strEditText)) {
((FontEditText) view).setTypeface(tfFontFace);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Jelaskan gaya dan atribut di masing-masing xml:
<!--FontTextView-->
<declare-styleable name="FontTextViewStyle">
<!-- Style of the FontTextView. -->
<attr name="ftvFontStyle" format="reference"/>
</declare-styleable>
<declare-styleable name="FontTextView">
<!-- Font face of FontTextView. -->
<attr name="ftvFontFace" format="reference"/>
</declare-styleable>
dan
<!--FontTextView-->
<style name="StyledFontTextView" parent="@android:style/Theme.Light">
<item name="ftvFontStyle">@style/DefaultFontTextView</item>
</style>
<style name="DefaultFontTextView">
<item name="ftvFontFace">@string/font_roboto_regular</item>
</style>
tentukan beberapa gaya lagi:
<style name="App_TextViewStyle" parent="@android:style/Widget.TextView">
<item name="android:textColor">@color/text_grey</item>
<item name="android:textSize">@dimen/sp_20</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="App_TextViewStyleMedium" parent="@android:style/Widget.TextView">
<item name="android:textColor">@color/text_hint</item>
<item name="android:textSize">@dimen/sp_18</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="App_TextViewStyleSmall" parent="@android:style/Widget.TextView">
<item name="android:textColor">@color/text_grey_light</item>
<item name="android:textSize">@dimen/sp_14</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
sebutkan font di strings.xml Anda:
...
<string name="font_roboto_regular">fonts/roboto_regular.ttf</string>
...
dan digunakan dalam tata letak menghemat beberapa kode dan waktu:
<com.mypackage.custom_views.FontTextView
style="@style/App_TextViewStyleMedium"
android:layout_gravity="start|bottom"
android:gravity="start|bottom"
app:fetFontFace="@string/font_roboto_regular"
android:text="@string/are_you_a" />
Di Android level 16 ke atas, semua ini disederhanakan, karena sekarang Anda dapat tetap menggunakan TTF dan resource font lainnya /res/font
folder, bukan di aset. Itu menghapus sebagian besar kelas, gaya, dan atribut khusus, lihat:
Sumber Daya Font di Android
Selamat Coding dengan gaya !! :-)