如何制作自定义 TextView?

     2023-04-13     160

关键词:

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

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

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

【问题讨论】:

参考this 【参考方案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_
                    android:layout_
                    android:text="@string/accountInfoText"
                    android:textColor="#727272"
                    android:textSize="18dp" />

【讨论】:

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

Textview 创建自定义视图。

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

    ……

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

    输入strings.xml中的所有字体

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

    创建一个资产文件夹并复制所有你想要放入字体文件夹的必要字体

    创建一个扩展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_
  android:text="Hello World Ankit"
  android:textSize="16sp"
  app:fontName="Roboto_Medium"
  android:layout_/>

在 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_
   android:layout_
   android:text="@string/accountInfoText"
   android:textColor="#727272"
   android:textSize="18dp" />

有了这个-

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

【讨论】:

如何使用图像和文本制作自定义 UIButton?

...:正如我在Android上所做的那样,在布局下安排ImageView和TextView并在布局上设置ClickListener,所以我如何在swift(3.0)Xcode8上做同样的事情。没有像XML这样的标记。拖放很难做到。如何使 查看详情

内部带有TextView的快速自定义UITableViewCell消失

】内部带有TextView的快速自定义UITableViewCell消失【英文标题】:swiftcustomUITableViewCellwithTextViewinsidedisappears【发布时间】:2015-06-1122:28:27【问题描述】:我正在尝试在我的tableView中获取一个带有textView的自定义tableView单元格。我制... 查看详情

如何添加 onCommit、isEditing 以及自定义绑定到自定义 textview 的能力?

】如何添加onCommit、isEditing以及自定义绑定到自定义textview的能力?【英文标题】:HowtoaddonCommit,isEditing,andabilitytohaveacustombindingtoacustomtextview?【发布时间】:2020-08-3018:25:25【问题描述】:提供以下UIViewRepresentableisEditing和onCommit属... 查看详情

Android自定义View(TextView+Button+一些自定义行为)?

】Android自定义View(TextView+Button+一些自定义行为)?【英文标题】:AndroidcustomView(TextView+Button+somecustomizedbehaviors)?【发布时间】:2012-05-2807:59:02【问题描述】:这应该很容易做到,但不知何故,经过大约15分钟的搜索,我仍然无... 查看详情

多个字体到单个自定义 TextView

】多个字体到单个自定义TextView【英文标题】:MultiplefontstoasinglecustomTextView【发布时间】:2018-07-2705:28:01【问题描述】:我们如何在单个自定义TextView中使用不同样式的Roboto(或任何其他)字体(Roboto-Regular、Roboto-Thin、Roboto-Bold... 查看详情

以编程方式结合 Textview 和 EditText 创建自定义字段

】以编程方式结合Textview和EditText创建自定义字段【英文标题】:CreateCustomFieldsProgrammaticallycombiningTextviewandEditText【发布时间】:2019-04-1611:01:51【问题描述】:我正在根据从API的JSON响应接收到的字符串制作动态表单。在这里我想... 查看详情

Android:隐藏自定义TextView的文本

】Android:隐藏自定义TextView的文本【英文标题】:Android:HideTextofcustomTextView【发布时间】:2013-08-2619:41:44【问题描述】:我有一个textView并且我不想打印文本,我想自己绘制它并隐藏打印的文本。如何隐藏打印出来的文字?编辑... 查看详情

如何在自定义 Listview 布局适配器中安排 Textview,放置在另一个 Textview 下方?添加了适配器支持

】如何在自定义Listview布局适配器中安排Textview,放置在另一个Textview下方?添加了适配器支持【英文标题】:HowcanIarrangeaTextview,inaCustomListviewLayoutAdapter,placedbeneathanotherTextview?Addedadaptersupport【发布时间】:2019-01-2719:33:13【问题描... 查看详情

如何在不使用自定义适配器的情况下从 ListView 获取特定的 TextView (View)?

...如何在不使用自定义适配器的情况下从ListView获取特定的TextView(View)?【英文标题】:HowtogetspecificTextView(View)fromaListViewwithoutuseofcustomAdapter?【发布时间】:2016-02-2615:27:09【问题描述】:这是我想要获得ListView的特定子视图的代码:... 查看详情

在使用xml的android TextView中使用自定义字体

】在使用xml的androidTextView中使用自定义字体【英文标题】:UsingcustomfontinandroidTextViewusingxml【发布时间】:2012-03-0818:46:18【问题描述】:我已将自定义字体文件添加到我的资产/字体文件夹中。如何从我的XML中使用它?我可以从如... 查看详情

每个项目中带有 Switch 和 TextView 的自定义 ListView

】每个项目中带有Switch和TextView的自定义ListView【英文标题】:CustomListViewwithSwitchandTextViewineachitem【发布时间】:2012-04-1600:07:53【问题描述】:我想要一个自定义ListView,其中包含一组用户可以订阅或取消订阅的队列。我的列表视... 查看详情

如何制作“特殊”风格的数字

...现在程序中?【问题讨论】:【参考方案1】:您可以为TextView使用自定义字体:Typefacetf=Typeface.createFromAsset(getAssets(),"font 查看详情

Android:自定义 TextView 膨胀异常

】Android:自定义TextView膨胀异常【英文标题】:Android:CustomTextViewInflateException【发布时间】:2013-04-2906:07:43【问题描述】:我创建了一个自定义TextView,我唯一做的就是在布局xml中放置了一个“实例”。这会导致应用程序因“Infla... 查看详情

如何在我绘制文本的自定义视图中使链接、电话号码可点击(与 textview 中的行为相同)?

...我绘制文本的自定义视图中使链接、电话号码可点击(与textview中的行为相同)?【英文标题】:Howtomakelink,phonenumberclickable(Samebehaviourasintextview)inacustomviewwhereidrawtext?【发布时间】:2017-11-1308:48:00【问题描述】:我创建了一个视... 查看详情

如何制作自定义组件属性?

】如何制作自定义组件属性?【英文标题】:howtomakecustomcomponentproperty?【发布时间】:2012-02-1516:58:51【问题描述】:我需要帮助来制作一个控件属性,当您单击它时,它会弹出一个自定义对话框,如设置。就像TPicture一样。有什... 查看详情

在自定义活动中绘制 TextView?

】在自定义活动中绘制TextView?【英文标题】:DrawaTextViewinsideacustomactivity?【发布时间】:2011-05-2011:20:44【问题描述】:我正在实现一个自定义View,我需要在其中绘制一些文本。文本必须适合一个盒子(所以我必须把它分解并使... 查看详情

如何制作自定义 IGListSectionController

】如何制作自定义IGListSectionController【英文标题】:HowtomakecustomIGListSectionController【发布时间】:2017-04-2413:21:38【问题描述】:我正在探索IGListKit,我正在尝试使用IGListSectionController子类来实现这一点:基本上,每个彩色区域都是... 查看详情

如何使用实时贴纸制作自定义相机

】如何使用实时贴纸制作自定义相机【英文标题】:HowtomakeacustomCamerawithliveStickers【发布时间】:2017-03-2010:20:01【问题描述】:我正在制作一个带有人脸检测功能的自定义相机,效果很好。但我想在录制/预览的面孔上添加贴纸。... 查看详情