Crear una vista personalizada para Textview
. Paso 1: realice la entrada en el archivo attrs.xml
y proporcione una opción para seleccionar Fuente como lista en TextView
personalizado.
<declare-styleable name="CustomFontTextView">
<attr name="fontName"/>
</declare-styleable>
Paso 2: Crear la entrada de enumeración con la lista de fuentes y asignar valores únicos
<attr name="fontName" format="enum">
<enum name="Roboto_Bold" value="1" />
<enum name="Roboto_Italic" value="2" />
<enum name="Roboto_Light" value="3" />
<enum name="Roboto_Medium" value="4" />
<enum name="Roboto_Regular" value="5" />
<enum name="Roboto_Thin" value="6" />
</attr>
Paso 3: Haga las entradas de todas las fuentes en strings.xml
<string name="Roboto_Bold">Roboto-Bold</string>
<string name="Roboto_Medium">Roboto-Medium</string>
<string name="Roboto_Light">Roboto-Light</string>
<string name="Roboto_Regular">Roboto-Regular</string>
<string name="Roboto_Thin">Roboto-Thin</string>
<string name="Roboto_Italic">Roboto-Italic</string>
Paso 4: crear carpeta de activos y copie toda la fuente necesaria que desea poner en la carpeta de fuente
Paso 5: Crear una cla ss extendiendo TextView
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by ANKIT
*/
public class CustomFontTextView extends TextView {
String customFont;
public CustomFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
style(context, attrs);
}
public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
style(context, attrs);
}
private void style(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.CustomFontTextView);
int cf = a.getInteger(R.styleable.CustomFontTextView_fontName, 0);
int fontName = 0;
switch (cf)
{
case 1:
fontName = R.string.Roboto_Bold;
break;
case 2:
fontName = R.string.Roboto_Italic;
break;
case 3:
fontName = R.string.Roboto_Light;
break;
case 4:
fontName = R.string.Roboto_Medium;
break;
case 5:
fontName = R.string.Roboto_Regular;
break;
case 6:
fontName = R.string.Roboto_Thin;
break;
default:
fontName = R.string.Roboto_Regular;
break;
}
customFont = getResources().getString(fontName);
Typeface tf = Typeface.createFromAsset(context.getAssets(),
"font/" + customFont + ".ttf");
setTypeface(tf);
a.recycle();
}
}
Puede usar esta clase personalizada de esta manera. .. utilizar su packagename.classname
<ankit.com.customui.CustomFontTextView
android:layout_width="match_parent"
android:text="Hello World Ankit"
android:textSize="16sp"
app:fontName="Roboto_Medium"
android:layout_height="wrap_content"/>
Consulte [esto] (http://developer.android.com/guide/topics/ui/themes.html) – Ghost