use*_*261 5 android android-edittext
我试图在一个自定义字体上实现EditText.有没有人有更好的方法,而不是我现在正在做的事情?
Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
edittext.setTypeface(myFont);
Run Code Online (Sandbox Code Playgroud)
因为我有很多EditText...
Sai*_*ate 19
public class CEditText extends EditText {
private Context context;
private AttributeSet attrs;
private int defStyle;
public CEditText(Context context) {
super(context);
this.context=context;
init();
}
public CEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
this.attrs=attrs;
init();
}
public CEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context=context;
this.attrs=attrs;
this.defStyle=defStyle;
init();
}
private void init() {
Typeface font=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
this.setTypeface(font);
}
@Override
public void setTypeface(Typeface tf, int style) {
tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
super.setTypeface(tf, style);
}
@Override
public void setTypeface(Typeface tf) {
tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
super.setTypeface(tf);
}
Run Code Online (Sandbox Code Playgroud)
以XML格式调用此类,如下所示
<yourpackagename.CEditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
</yourpackagename.CEditText>
Run Code Online (Sandbox Code Playgroud)
创建扩展一个新的类EditText象
public class CustomEditTextNormal extends EditText
{
public CustomEditTextNormal(Context context)
{
super(context);
init(context);
}
public CustomEditTextNormal(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
public CustomEditTextNormal(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(context);
}
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
}
public void init(Context context)
{
try
{
Typeface myFont = Typeface.createFromAsset(context.getAssets(), "fonts/myfont.ttf");
setTypeface(mSearchAndSend.HelveticaLight);
}
catch (Exception e)
{
Logger.LogError(e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
并将其包含在您的XML上
<com.package.name.CustomEditText/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11964 次 |
| 最近记录: |