我在我的项目中使用它可能会帮助你。
TextViewEx.java
public class TextViewEx extends TextView {
public enum TextStyle {BOLD,LIGHT,REGULAR,SEMIBOLD,EXOREGULAR,BOLDLARGE};
TypedArray Canvasattrs=null;
int CurvatureDegree;
boolean isCurvature = false;
String direction;
String RVal;
public TextViewEx(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public TextViewEx(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs){
if (!this.isInEditMode()) { // used for preview while designing.
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextView_TypeFace)
Typeface font = null;
String Type = a.getString(R.styleable.TextView_TypeFace_TypeFace);
if(Type == null){
UserTypeFace.SetRegular(this); //Set Default Font if font is not defined in xml
return;
}
setStyle(Type);
} else {
setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
}
}
private void setStyle(String style){
TextStyle value = TextStyle.valueOf(style); //convert String to ENUM
switch (value){
case BOLD:UserTypeFace.SetBold(this);
break;
case LIGHT:UserTypeFace.Setlight(this);
break;
case REGULAR:UserTypeFace.Setthin(this);
break;
case SEMIBOLD:UserTypeFace.SetSEMIBOLD(this);
break;
case EXOREGULAR:UserTypeFace.SetRegular(this);
break;
case BOLDLARGE:UserTypeFace.SetBoldLarge(this);
break;
}
}
}
UserTypeFace.java
public class UserTypeFace {
public static final String BOLD;
public static final String LIGHT;
public static final String REGULAR;
public static final String EXOREGULAR;
public static final String BOLDLARGE;
public static final String SEMIBOLD;
static {
REGULAR="fonts/JosefinSans-Bold.ttf";
LIGHT="fonts/JosefinSans-Bold.ttf";
BOLD="fonts/JosefinSans-Bold.ttf";
SEMIBOLD="fonts/JosefinSans-Bold.ttf";
BOLDLARGE="fonts/JosefinSans-Bold.ttf";
EXOREGULAR="fonts/Exo2-Regular.ttf";
}
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
private static Typeface getTypeFace(Context context, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface typeFace = Typeface.createFromAsset(
context.getAssets(), assetPath);
cache.put(assetPath, typeFace);
} catch (Exception e) {
Log.e("TypeFaces", "Typeface not loaded.");
return null;
}
}
return cache.get(assetPath);
}
}
public static void Setthin(TextView obj){
obj.setTypeface(getTypeFace(obj.getContext(),EXOREGULAR), Typeface.NORMAL);
}
public static void Setlight(TextView obj){
obj.setTypeface(getTypeFace(obj.getContext(),LIGHT), Typeface.NORMAL);
}
public static void SetBold(TextView obj){
obj.setTypeface(getTypeFace(obj.getContext(),BOLD), Typeface.NORMAL);
}
public static void SetSEMIBOLD(TextView obj){
obj.setTypeface(getTypeFace(obj.getContext(),SEMIBOLD), Typeface.NORMAL);
}
public static void SetRegular(TextView obj){
obj.setTypeface(getTypeFace(obj.getContext(),EXOREGULAR), Typeface.NORMAL);
}
public static void SetBoldLarge(TextView obj){
obj.setTypeface(getTypeFace(obj.getContext(),BOLDLARGE), Typeface.BOLD);
}
public static Typeface getRegular(View obj){
return getTypeFace(obj.getContext(),EXOREGULAR);
}
}
添加这attrs.xml
<resources>
<declare-styleable name="TextView_TypeFace">
<attr name="TypeFace" format="reference|string" localization="suggested" />
</declare-styleable>
</resources>
,你会在布局文件中像
来增加这个
<pakage.name.TextViewEx
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:textColor="#282828"
attrs:TypeFace="@string/Bold"
android:textSize="30sp"
android:text=" Please Login "
android:id="@+id/txtlogin" />
非常感谢您先生 – 2015-02-06 09:47:46
高兴地帮助:) – Muthu 2015-02-06 09:53:24