English 中文(简体)
在 Android 中更改编辑文字的字体吗?
原标题:Change font for EditText in Android?

有办法更改 Android 中的编辑图文的字体吗? 我要它匹配我为我所有文本“ 意见” 设定的字体 。

最佳回答
 editText.setTypeface(Typeface.SERIF); 

就像文本视图一样。

 <TextView
  ...
     android:typeface="serif"
  ... />

编辑:以上是 XML

问题回答

解答1: 只需将这些方法用父母的过往观点称为论据。

private 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) {
            ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font.ttf"));
        }
    } catch (Exception e) {
 }
}

解答 2: : 您可以使用自定义字体将 TextView 类分类, 并使用它而不是文本视图 。

public class MyEditView extends EditText{

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

    public MyEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyEditView(Context context) {
        super(context);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font.ttf");
            setTypeface(tf);
        }
    }

 }

assets 文件夹上创建 fonts 文件夹, 并放置您的. ttf 字体文件, 然后在 Create () 函数写入 :

EditText editText =(EditText)findViewById(R.id.insert_yors_edit_text_layout);
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/yours_font.ttf"); 
editText.setTypeface(type);

您可以在最后一个版本的 Restrol 工作室 3 的 Res 目录下创建 < strong > font 文件夹 。 然后复制并复制您 r 的字体到 < strong > font < / strong > 文件夹 。 现在只需将 < code > font家庭 添加到 < strong > EditText 标签 xml 。

如下文。

        <EditText
            android:id="@+id/subject"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/headerShare"
            android:layout_marginBottom="5dp"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:background="@drawable/edittext_bg"
            android:fontFamily="@font/ritaric"
            android:gravity="start"
            android:hint="Subject"
            android:inputType="text"
            android:maxHeight="50dp"
            android:padding="16dp"
            android:textColor="@color/black"
            android:textColorHint="@color/black"
            android:textSize="@dimen/colors_textview_size" />

我们使用 android:fontFamily="@font/ritaric" 来在 EditText 上应用字体 。

void setFont(Context context, ViewGroup vg)
        {
          final String FONT_NAME = "lato_bold.ttf";
            for (int i = 0; i < vg.getChildCount(); i++)
                {
                    View v = vg.getChildAt(i);
                    if (v instanceof ViewGroup)
                        setFont(context, (ViewGroup) v);
                    else if (v instanceof TextView)
                        {
                            ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME ));
                        }
                    else if (v instanceof EditText)
                        {
                            ((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME ));
                        }
                    else if (v instanceof Button)
                        {
                            ((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME ));
                        }
                    else if (v instanceof CheckBox)
                        {
                            ((CheckBox) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME ));
                        }
                    else if (v instanceof RadioButton)
                        {
                            ((RadioButton) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME ));
                        }
                }
        }

在您的活动或片段中,获取主版式

Inflater inflater = (Inflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//For Activity
//Inflater inflater = (Inflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.main_fragment, container, false);
//For Activity
//View v = inflater.inflate(R.layout.signup_fragment, null, false);
if (v instanceof ViewGroup)
    {
      setFont(getActivity(), (ViewGroup) v);
      //For Activity
     //setFont(this, (ViewGroup) v);
    }

First Method

在您的java 类中使用此代码

    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. 创建一个新的 java 类, 并按您想要的

    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. 在您的 Xml 文件中使用它

                  <YourPackageNAme.CustomEditText
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:background="@null"/>
    

Add this code in your java file Which type you want you can add like NORMAL, ITALIC, BOLD, BOLD_ITALIC.

编辑文本. setTypeface( 缩写、 typeface. ITALIC) ;

lil late but, Simple way

 mEditTextMobileNumber.setTextSize(16.0f);

如果您想要在打字时更改字体 :

   // Change text size when started entering number
    mEditTextMobileNumber.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s == null || String.valueOf(s).equalsIgnoreCase("")) {
                mEditTextMobileNumber.setTextSize(12.0f);
            } else {
                mEditTextMobileNumber.setTextSize(20.0f);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签