如何制作自定义的TextView?

41

我正在尝试创建一个自定义文本视图,并设置从给定路径中获取的字体。请提供任何示例以及如何用更少的代码实现:

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

2
请参考此链接:http://developer.android.com/guide/topics/ui/themes.html。 - Ghost
3个回答

102
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" />

34
我建议使用单例来应用字体,以避免每次从资产中创建字体。 - JackMahoney
@JackMahoney 感谢您非常有帮助的评论。现在我的应用程序运行非常快!! - Hamzeh Soboh
@JackMahoney,你有将自定义视图转换为单例并展示给其他人的示例吗?这会很有帮助。 - kabuto178
6
这是一些伪代码,可以帮助您了解 https://gist.github.com/jackmahoney/9181787 - JackMahoney
1
最好在这里使用AppCompatTextView,如果不使用它,甚至会收到警告,实现方式完全相同,只需扩展AppCompatTextView而不是TextView。 - Trevor Hart
1
根据AndroidX的规定,您不能扩展TextView。您应该使用AppCompactTextView。 - Arpit Patel

49

TextView创建自定义视图。

  1. attrs.xml文件中添加条目,并在自定义TextView中提供选择字体的选项列表。

  2. 创建一个枚举条目,其中包含字体列表并分配唯一值。

  3. strings.xml中记录所有字体的条目:

    Roboto-Bold Roboto-Medium Roboto-Light Roboto-Regular Roboto-Thin Roboto-Italic

  4. 创建一个资源文件夹并将所有要放入字体文件夹中的必要字体复制到该文件夹中。

  5. 创建一个继承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"/>

在res/values/attrs.xml文件中添加以下可样式化内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomFontTextView">
        <attr name="fontName" format="string" />
    </declare-styleable>
</resources>

9
比被接受的答案更好的实现。 - Rahul Thakur

0
如果你在写Kotlin代码,这就是你可以做的方法。 首先,将你的字体添加到font文件夹中。 然后,创建一个独立的Kotlin类。给它起个名字,比如HeadlineTextView(或任何能暗示出TextView用途的名字)。
class HeadlineTextView(context: Context, attributeSet: AttributeSet) : AppCompatTextView(context, attributeSet) {

  init {
      applyFont()
  }

  private fun applyFont() {
    val headlineTypeface: Typeface=Typeface.create("Montserrat", Typeface.NORMAL)
    typeface=headlineTypeface
  }

}

现在在你的XML中使用这个自定义文本视图。 替换这个-

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

通过这个 -

<com.gmail.something.myapp.HeadlineTextView
   android:id="@+id/textView2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/accountInfoText"
   android:textColor="#727272"
   android:textSize="18dp" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接