【问题标题】:How to make a custom TextView?如何制作自定义 TextView?
【发布时间】:2012-03-17 15:40:21
【问题描述】:

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

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

【问题讨论】:

标签: android textview


【解决方案1】:
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" />

【讨论】:

  • 我建议使用单例来应用字体以避免每次都从资产创建字体。
  • @JackMahoney +1 非常有用的评论。现在我的申请速度非常快!!
  • @JackMahoney 你有将自定义视图转换为单例以显示的示例吗?会有帮助的。
  • @kabuto178 这里有一些伪代码可以让你有个想法gist.github.com/jackmahoney/9181787
  • 这里最好使用AppCompatTextView,不使用甚至会收到警告,实现方式完全相同,只是扩展AppCompatTextView而不是TextView。
【解决方案2】:

Textview 创建自定义视图。

  1. attrs.xml 文件中创建条目,并提供一个选项以在自定义TextView 中选择字体作为列表。

    ……
  2. 使用字体列表创建枚举条目并分配唯一值

  3. 输入strings.xml中的所有字体

    Roboto-Bold 中型机器人 机器人灯 机器人-常规 机器人薄 机器人斜体

  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>

【讨论】:

  • 比接受的答案更好的实现。
【解决方案3】:

如果您使用 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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-17
    • 2016-02-16
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多