2012-02-28 33 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