Change font for EditText in Android?

前端 未结 8 941
梦谈多话
梦谈多话 2020-12-10 10:17

Is there any way to change the font for a EditText in Android? I want it to match the font\'s I set for all my textViews.

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 10:51

    First Method

    use this code in your java class

        EditText et_brand=(EditText)findViewById(R.id.et_brand);
        et_brand.setTypeface(Typeface.createFromAsset(getAssets(),"Aspergit Bold.ttf")); 
        //Aspergit Bold.ttf is Font style name of text
    

    Second Method

    1. Create a new java class and it as u want

      public class CustomTextView extends TextView {
      
      public CustomTextView(Context context, AttributeSet attrs, int defStyle) 
      {
      super(context, attrs, defStyle);
      init();
      }
      
      public CustomTextView(Context context, AttributeSet attrs) {
          super(context, attrs);
      init();
      }
      
      public CustomTextView(Context context) {
      super(context);
      init();
      }
      
      private void init() {
         Typeface tf = Typeface.createFromAsset(getContext().getAssets(), 
      "Aspergit Bold.ttf");
      setTypeface(tf);
      }
      }
      
    2. Use it in your Xml file

                    
      

提交回复
热议问题