由网友(念旧回不到曾经)分享简介:我试图让具有字体从给定路径设置自定义文本视图。请为我提供任何例子,伊灿如何作出这样的少code:<的TextView机器人:ID =@ + ID / textView2机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:文本=@字符串/...
我试图让具有字体从给定路径设置自定义文本视图。请为我提供任何例子,伊灿如何作出这样的少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" />
相关推荐
最新文章