TextView 支持自定義字體和屬性

使用android 系統字體:

    Android系統默認支持三種字體,分別爲:“sans”, “serif”,  “monospace

eg.

         <!--  使用默認的sans字體-->
        <TextView    android:id="@+id/sans"
                    android:text="Hello,World"
                    android:typeface="sans"
                    android:textSize="20sp">

        </TextView>

使用自定義字體屬性:

<declare-styleable name="CustomizeTextView">     

      <attr name="fontType">

           <enum name="none" value="0" />

           <enum name="regular" value="1" />

         <enum name="bold" value="2" />

     </attr>  

   </declare-styleable>

xmls:namespace="http://schemas.android.com/apk/res/project.name.space"

         <!--  使用自定義字體屬性-->
        <CustomizeTextView    android:id="@+id/sans"
                    android:text="Hello,World"
                    namespace:fontType="regular"
                    android:textSize="20sp">

        </TextView>

使用自定義字體:

             public class CustomizeTextView extends TextView{

pulibc static TypeFace   typeface1;

public static TypeFace typeface2;

{

      typeface1 = Typeface.createFromAsset(context.getAssetManager(), "font/text1.ttf");

      typeface2 = Typeface.createFromAsset(context.getAssetManager(), "font/text2.ttf");

}

private void init(Context context , AttributeSet attrs , int defStyle)

{

TypedArray a = context.obtainStyledAttributes(attrs,  R.styleable.MyView);

int fontType = a.getInt(R.styleable.CustomizeTextView_fontType,default);

a.recycle();

switch(fontType){

case 1:

setTypeFace(typeface1);

break;

case 2:

setTypeFace(typeface2);

break;

default:

           break;

}

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章