2014-08-30 59 views
2

如何獲取自定義TextView的屬性設置字體爲Textview的自定義TextView屬性。 基於屬性值的TextView從XML獲取自定義文本視圖的自定義屬性

public class MyTextView extends TextView 
{ 
    public MyTextView(Context context, AttributeSet attrs, int defStyle) 
    { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    public MyTextView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     init(); 
    } 

    public MyTextView(Context context) 
    { 
     super(context); 
     init(); 
    } 

    public void init() 
    { 
      // set font_name based on attribute value of textview in xml file 
      String font_name = ""; 
     if (!isInEditMode()) 
     { 
      Typeface tf = Typeface.createFromAsset(getContext().getAssets(), 
        "fonts/"+font_name); 
      setTypeface(tf); 
     } 
    } 

設置字體在XML文件中

<com.Example.MyTextView 
      android:id="@+id/header" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      fontname="font.ttf" 
      android:text="Header" 
/> 

我也把font.ttf文件中的資產 - >字體 謝謝

回答

2

首先你java的定義declarable-styleable這應該是這樣的:

<declare-styleable name="MyTextView"> 
    <attr name="fontname" format="string"/> 
</declare-styleable> 

在你的佈局,你可以「訪問」這個ATTR,但首先你必須定義一個namespace

xmlns:myPrefix="http://schemas.android.com/apk/res-auto" 

注意:namespace是任意的。所以你可以把它命名爲xmlns:whatever

然後你就可以設置字體名這樣的:

<com.Example.MyTextView 
    android:id="@+id/header" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    myPrefix:fontname="font.ttf" 
    android:text="Header" 
/> 

爲了找回你必須做的MyTextView建設者以下值:

TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.MyTextView); 
String font = values.getString(R.styleable.MyTextView_fontname); 

// set the typeface etc. 

注意:MyTextView_fontname這總是一個用下劃線分隔的組合。所以基本結構是StyleableName _ AttrName

5

1。將readAttr(context,attrs)方法添加到您的構造函數中,如下所示。

public MyTextView(Context context, AttributeSet attrs, int defStyle) 
{ 
    super(context, attrs, defStyle); 
    readAttr(context,attrs); 
    init(); 
} 

public MyTextView(Context context, AttributeSet attrs) 
{ 
    super(context, attrs); 
    readAttr(context,attrs) 
    init(); 
} 

public MyTextView(Context context) 
{ 
    super(context); 
    init(); 
} 

2。在同一個類中定義readAttr()方法。

private void readAttr(Context context, AttributeSet attrs) { 
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyTextView); 

    // Read the title and set it if any 
    String fontName = a.getString(R.styleable.MyTextView_fontname) ; 
    if (fontName != null) { 
     // We have a attribute value and set it to proper value as you want 
    } 

    a.recycle(); 
} 

3。修改attrs.xml文件(RES /值/ attrs.xml)並添加以下到文件

<declare-styleable name="MyTextView"> 
    <attr name="fontname" format="string" /> 
</declare-styleable> 

4。在Xml文件中。

<com.Example.MyTextView 
    android:id="@+id/header" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    custom:fontname="font.ttf" 
    android:text="Header" /> 

5。將此行添加到xml文件的頂部容器。

xmlns:custom="http://schemas.android.com/apk/res/com.yourpackage.name" 

這就是所有

0

第一添加自定義的字體資產 - > fonts目錄 Like this image

然後添加以下代碼到attrs.xml文件夾值

<declare-styleable name="TextViewCustomFont"> 
    <attr name="showText" format="boolean" /> 
    <attr name="custom_font" format="enum"> 
     <enum name="roboto_Regular" value="0"/> 
     <enum name="roboto_bold" value="1"/> 
     <enum name="roboto_medium" value="2"/> 

    </attr> 
</declare-styleable> 

現在添加這個自定義類擴展了TextView。一件事是,默認值是機器人定期並且它具有默認值0,從而當自定義:custom_font屬性未設定爲比a.getInteger(R.styleable.TextViewCustomFont_custom_font,0)返回0。

public class CustomFontTextView extends TextView { 


    public CustomFontTextView(Context context) { 
     super(context); 


    } 

    public CustomFontTextView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(context,attrs); 

    } 

    public CustomFontTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context,attrs); 



    } 

    private void init(Context context, AttributeSet attrs) { 
     int fontFlag; 
     TypedArray a = context.getTheme().obtainStyledAttributes(
       attrs, 
       R.styleable.TextViewCustomFont, 
       0, 0); 

     try { 

      fontFlag = a.getInteger(R.styleable.TextViewCustomFont_custom_font, 0); 
      Log.v("fontFlag", fontFlag + ""); 
     } finally { 
      a.recycle(); 
     } 

     Typeface tf = null; 

     if (fontFlag == 0) 
      tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf"); 
     else if (fontFlag == 1) 
      tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Bold.ttf"); 
     else if (fontFlag == 2) 
      tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Medium.ttf"); 


     setTypeface(tf); 

    } 
} 

現在使用這個類在你的佈局文件

<com.example.tomal.CustomFontTextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_vertical" 

        android:text="@string/title" 
        your_choice:custom_font="roboto_medium" 


        /> 

添加到線到父佈局

xmlns:your_choice="http://schemas.android.com/apk/res-auto"