如何在Android中为EditText设置自定义字体?

6

我正在尝试在EditText上实现自定义字体。有没有比我当前做法更好的方法?

Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
edittext.setTypeface(myFont);

因为我有很多 EditText...


1
你的问题是什么? - ashishmaurya
8个回答

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

在XML中调用此类的方法如下:

<yourpackagename.CEditText  android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp">
</yourpackagename.CEditText>

哇!太棒了!但我想知道@SainathPatwarykarnate....如何使每个单词定义那个EditText的样式? - gumuruh
如果你真的想让每个单词都有不同的单一编辑文本样式,那总是取决于单词长度等很多其他因素,所以最好你维护一个参数化方法,根据方法的不同值改变样式。目前我还没有为此定义出解决方案。 - Sainath Patwary karnate
对于你所说的概念/流程,我明白 @SainathPatwarykarnate.... 是的,我认为我们最好进入代码来使其变得真实 :D - gumuruh
请您详细说明您的意思。 - Sainath Patwary karnate

6

创建一个新的类,扩展 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);
    }
  }
}

并在你的XML中包含它,如下:

<com.package.name.CustomEditText/>

不要捕获异常 - Tyler

0
你需要在Common类中创建一个方法,例如:
public void setExternalFonts(EdiText tv) {
  Typeface tf = Typeface.createFromAsset(context.getAssets(),
            "fonts/myfont.ttf");
  tv.setTypeface(tf);
}

现在调用这个方法:

yourClassName.setExternalFonts(yourEditText);

我尝试过这个,但在设置路径时出现了一些问题。我已经像这样设置了路径: Typeface tf = Typeface.createFromAsset(getAssets(), "/assets/roboto_regular.ttf");你能让我知道我错在哪里吗? - Yesha Shah
如果字体文件只放在assets文件夹中,那么只需将“/assets/roboto_regular.ttf”更改为“roboto_regular.ttf”。请展示您的字体结构。 - Piyush
似乎字体没有改变,它们仍然是原来的样子。可能出了什么问题?@Piyush - Yesha Shah
你是在以编程方式处理它吗? - Piyush
我需要将自定义字体放在提示中,而不是在提示上。 - Yesha Shah
显示剩余2条评论

0
要不创建一个继承EditText的新类,设置你想要的字体,然后在xml中实例化这个新类怎么样?

0
创建一个继承自EditText的新类,然后重写public void setTypeface(Typeface tf, int style)方法并添加你自己的字体。

0
创建一个继承自EditText的新类,然后重写public void setTypeface(Typeface tf, int style)方法并添加您自己的字体。
在您的活动中使用如下: FontLoader.setQuickSandTypeface(YourEditText)

0

试试这个

public void overrideFonts(final Context context, final View v) {
    try {
        if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;
            for (int i = 0; i < vg.getChildCount(); i++) {
                View child = vg.getChildAt(i);
                overrideFonts(context, child);
            }
        } else if (v instanceof EditText) {
            ((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "roboto_thin.ttf"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

0

试试这个。对于按钮、文本视图等都很有用!

<your.namespace.app.FontEditText
     app:font="montserrat"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>

怎么可能?就是这样!

public class FontEditText extends EditText {

public FontEditText(Context context) {
    this( context, null );
}

public FontEditText(Context context, AttributeSet attrs) {
    this( context, attrs, 0 );
    init( context, attrs );
}

public FontEditText(Context context, AttributeSet attrs, int defStyle) {
    super( context, attrs, defStyle );
    init( context, attrs );
}

public FontEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super( context, attrs, defStyleAttr, defStyleRes );
    init( context, attrs );
}

private void init(Context context, AttributeSet attrs) {
    TypedArray ta = context.obtainStyledAttributes( attrs, R.styleable.Fonts );

    if ( ta != null ) {
        String fontAsset = ta.getString( R.styleable.Fonts_font );
        if ( !StringUtils.isEmpty( fontAsset ) ) {
            int type = Integer.parseInt( fontAsset );

            Typeface typeFace = FontManager.getInstance( context ).getByType( type );
            ta.recycle();
            super.setTypeface( typeFace );
        }
    }
}

}

public class FontManager {

private static FontManager Instance;

private Context context;

private Typeface robotoCondensedBold;
private Typeface robotoCondensed;
private Typeface robotoLight;
private Typeface kronica;
private Typeface montserrat;
private Typeface montserratLight;
private Typeface keepCalmMedium;

private FontManager(Context context) {
    this.context = context;
    this.robotoCondensedBold = Typeface.createFromAsset( context.getAssets(), "fonts/RobotoCondensed-Bold.ttf" );
    this.robotoCondensed = Typeface.createFromAsset( context.getAssets(), "fonts/RobotoCondensed-Regular.ttf" );
    this.robotoLight = Typeface.createFromAsset( context.getAssets(), "fonts/Roboto-Light.ttf" );
    this.kronica = Typeface.createFromAsset( context.getAssets(), "fonts/kronika.ttf" );
    this.montserrat = Typeface.createFromAsset( context.getAssets(), "fonts/Montserrat-Regular.ttf" );
    this.montserratLight = Typeface.createFromAsset( context.getAssets(), "fonts/Montserrat-Light.ttf" );
    this.keepCalmMedium = Typeface.createFromAsset( context.getAssets(), "fonts/KeepCalmMedium.ttf" );
}

public synchronized static FontManager getInstance(Context context) {
    if ( Instance == null )
        Instance = new FontManager( context );

    return Instance;
}

public Typeface getByType(int type) {
    switch ( type ) {
        case 0:
            return FontManager.getInstance( context ).getRobotoCondensedBold();
        case 1:
            return FontManager.getInstance( context ).getRobotoLight();
        case 2:
            return FontManager.getInstance( context ).getKronica();
        case 3:
            return FontManager.getInstance( context ).getRobotoCondensed();
        case 4:
            return FontManager.getInstance( context ).getMontserrat();
        case 5:
            return FontManager.getInstance( context ).getMontserratLight();
        case 6:
            return FontManager.getInstance( context ).getKeepCalmMedium();
        default:
            return Typeface.DEFAULT;
    }
}

public Typeface getRobotoCondensedBold() {
    return robotoCondensedBold;
}

public Typeface getKronica() {
    return kronica;
}

public Typeface getRobotoCondensed() {
    return robotoCondensed;
}

public Typeface getRobotoLight() {
    return robotoLight;
}

public Typeface getMontserrat() {
    return montserrat;
}

public Typeface getMontserratLight() {
    return montserratLight;
}

public Typeface getKeepCalmMedium() {
    return keepCalmMedium;
}

此外,在您的res文件夹中还有一个font_attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Fonts">
        <attr name="font" format="enum">
            <enum name="robotoCondensedBold" value="0"/>
            <enum name="robotoLight" value="1"/>
            <enum name="kronica" value="2"/>
            <enum name="robotoCondensed" value="3"/>
            <enum name="montserrat" value="4"/>
            <enum name="montserratLight" value="5"/>
            <enum name="keepCalmMedium" value="6"/>
        </attr>
    </declare-styleable>
</resources> 

请注意,您只需要修改FontManagerfont_attrs.xml即可自定义字体!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接