2012-02-28 32 views
23

我想製作一個自定義的文本視圖,它具有從給定路徑設置的字體。請給我提供任何例子,我怎麼能作出這樣用更少的代碼:如何製作自定義TextView?

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/accountInfoText" 
    android:textColor="#727272" 
    android:textSize="18dp" /> 
+1

請參考[這裏](http://developer.android.com/guide/topics/ui/themes.html) – Ghost 2012-02-28 06:21:48

回答

83
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Typeface; 
import android.util.AttributeSet; 
import android.widget.TextView; 

public class FontTextView extends TextView { 


   public FontTextView(Context context) { 
     super(context); 
     Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf"); 
     this.setTypeface(face); 
   } 

   public FontTextView(Context context, AttributeSet attrs) { 
       super(context, attrs); 
    Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf"); 
 this.setTypeface(face); 
   } 

   public FontTextView(Context context, AttributeSet attrs, int defStyle) { 
       super(context, attrs, defStyle); 
    Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf"); 
 this.setTypeface(face); 
   } 

   protected void onDraw (Canvas canvas) { 
       super.onDraw(canvas); 
        
      
   } 

} 

和XML:

<com.util.FontTextView 
                   android:id="@+id/textView2" 
                   android:layout_width="wrap_content" 
                   android:layout_height="wrap_content" 
                   android:text="@string/accountInfoText" 
                   android:textColor="#727272" 
                   android:textSize="18dp" /> 
+28

我建議使用一個單獨的應用的字體,以避免每次都從資產創建字體。 – JackMahoney 2013-04-26 06:04:52

+0

@JackMahoney +1對你非常有幫助的評論。現在我的應用程序非常快! – 2014-01-20 11:18:04

+0

@JackMahoney你有沒有將自定義視圖轉換爲單例顯示的例子?會有幫助。 – kabuto178 2014-02-20 20:39:54

-1

下面的自定義類可以幫助您自定義設置您所需的字體在TextView,所以你必須把一些.ttf文件的資產,並給自定義TextView這個路徑。

public class TextViewBoldFont extends TextView { 
    public TextViewBoldFont(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     String fontPath = "GOTHICB.TTF"; 
     Typeface fontsStyle = Typeface.createFromAsset(context.getAssets(), fontPath); 
     this.setTypeface(fontsStyle,Typeface.BOLD); 
    } 

    public TextViewBoldFont(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 

     String fontPath = "GOTHICB.TTF"; 
     Typeface fontsStyle = Typeface.createFromAsset(context.getAssets(), fontPath); 
     this.setTypeface(fontsStyle,Typeface.BOLD); 
    } 

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

     String fontPath = "GOTHICB.TTF"; 
     Typeface fontsStyle = Typeface.createFromAsset(context.getAssets(), fontPath); 
     this.setTypeface(fontsStyle,Typeface.BOLD); 
    } 
} 
+2

嗨,歡迎來到SO。請不要轉儲代碼作爲答案,解釋您的思路,以便用戶能夠理解您在做什麼。謝謝。 – Cthulhu 2016-03-16 15:28:00

18

Textview創建自定義視圖。 步驟1 - 在attrs.xml文件中輸入條目,並給出一個選項,以在自定義TextView中選擇字體作爲列表。

<declare-styleable name="CustomFontTextView"> 
    <attr name="fontName"/> 
</declare-styleable> 

第2步:創建字體列表枚舉項和分配唯一值

<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> 

第3步:在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> 

步驟4的所有字體的條目:創建資產文件夾並複製你想放在字體文件夾中的所有必要字體

第5步:創建一個cla ss延伸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(); 
    } 
} 

你可以這樣使用這個自定義類。 ..用你的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"/> 
+3

比接受的答案好得多。 – 2017-02-03 12:01:42