如何使自定义的TextView? [英] How to make custom textview?

查看:114
本文介绍了如何使自定义的TextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让具有字体从给定路径设置自定义文本视图。请为我提供任何例子,伊灿如何作出这样的少code:

 <的TextView
   机器人:ID =@ + ID / textView2
   机器人:layout_width =WRAP_CONTENT
   机器人:layout_height =WRAP_CONTENT
   机器人:文本=@字符串/ accountInfoText
   机器人:文字颜色=#727272
   机器人:TEXTSIZE =18dp/>
 

解决方案

 进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Typeface;
进口android.util.AttributeSet;
进口android.widget.TextView;

公共类FontTextView扩展TextView的{


 公共FontTextView(上下文的背景下){
 超(上下文);
 字体脸= Typeface.createFromAsset(context.getAssets(),Helvetica_Neue.ttf);
 this.setTypeface(面);
 }

 公共FontTextView(上下文的背景下,ATTRS的AttributeSet){
 超(背景下,ATTRS);
 字体脸= Typeface.createFromAsset(context.getAssets(),Helvetica_Neue.ttf);
 this.setTypeface(面);
 }

 公共FontTextView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
 超(背景下,ATTRS,defStyle);
 字体脸= Typeface.createFromAsset(context.getAssets(),Helvetica_Neue.ttf);
 this.setTypeface(面);
 }

 保护无效的OnDraw(帆布油画){
 super.onDraw(画布);
 
 
 }

}
 

和XML:

 < com.util.FontTextView
 机器人:ID =@ + ID / textView2
 机器人:layout_width =WRAP_CONTENT
 机器人:layout_height =WRAP_CONTENT
 机器人:文本=@字符串/ accountInfoText
 机器人:文字颜色=#727272
 机器人:TEXTSIZE =18dp/>
 

I'm trying to make a custom text view that has the font set from a given path. please provide me any example and how Ican make that with less code:

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

解决方案

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);
        
       
    }

}

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

这篇关于如何使自定义的TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆