【问题标题】:How to set custom fonts on EditText in Android?如何在 Android 中的 EditText 上设置自定义字体?
【发布时间】:2014-07-01 00:02:10
【问题描述】:

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

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

因为我有很多EditTexts...

【问题讨论】:

  • 那你有什么问题

标签: android android-edittext


【解决方案1】:
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....如何让每个单词定义该编辑文本的每种样式?
  • 如果你真的希望每个单词都应该有不同风格的单个编辑文本,这总是取决于单词长度..和许多其他的东西,所以最好你保持一个改变风格的参数化方法基于该方法的不同值,到目前为止我还没有任何定义的解决方案
  • 对于概念/流程我理解你在说什么@SainathPatwarykarnate....是的,我认为我们最好进入代码以使其成为现实:D
  • 你能详细说明你的意思吗
【解决方案2】:

创建一个扩展 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/>

【讨论】:

  • 不要捕获异常
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      你需要在 Common 类中创建一个方法 Like,

      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");你能告诉我我哪里错了吗?
      • 如果它只放在 assets 文件夹中,那么只需将 /assets/roboto_regular.ttf 更改为 roboto_regular.ttf。显示您的字体结构。
      • 似乎字体没有改变,它们保持原样。可能是什么问题? @Piyush
      • src>main>assets>roboto_regular.ttf这种结构是有的。
      • 您是否以编程方式进行操作?
      【解决方案6】:

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

      并像这样在您的活动中使用 FontLoader.setQuickSandTypeface(YourEditText) 在你的活动中

      【讨论】:

        【解决方案7】:

        试试这个

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

        【讨论】:

          【解决方案8】:

          试试这个。也适用于 Buttons、TextViews.. 随便!

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

          此外,font_attrs.xml 在您的res 文件夹中:

          <?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 即可自定义您的字体!

          【讨论】:

            猜你喜欢
            • 2013-12-15
            • 2017-01-20
            • 2016-06-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-04-15
            相关资源
            最近更新 更多