更改浮动标签EditText和TextInputLayout的字体

问题描述 投票:23回答:11

有人试图改变浮标的字体吗?我更改了EditText的源代码,但浮动标签的字体没有改变,我非常感谢那些帮助我的人

码:

               <android.support.design.widget.TextInputLayout
                    android:id="@+id/tilTextoDescricao"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/tilValorUnidade"
                    android:layout_marginTop="10dp">

                    <EditText
                        android:id="@+id/etTextoDescricao"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:hint="Descrição"
                        android:textSize="15dp"
                        android:inputType="text" />

                </android.support.design.widget.TextInputLayout>

----------------- 

   etTextoDescricao= (EditText) findViewById(R.id.etTextoDescricao);
  etTextoDescricao.setTypeface(CustomTypeface.getTypefaceMediumDefault(this));

android android-edittext android-design-library android-textinputlayout material-components
11个回答
28
投票

Design Library v23开始,你可以使用TextInputLayout#setTypeface()

这将在展开和浮动提示上设置字体。

这是在feature request上讨论的b.android.com

编辑:错误视图字体没有设置,但现在fixedv25.1.0


0
投票

我正在寻找这个,我发现这样,使用支持库:

Documentation

并将此字体设置为TextInputLayout。

对我来说就像魅力一样,我希望它能帮助别人=]

资料来源:<style name="TextInputLayoutErrorStyle" parent="TextAppearance.Design.Error"> <item name="fontFamily">@font/iran_sans_medium</item> <item name="android:fontFamily">@font/iran_sans_medium</item> </style> <style name="TextInputLayoutHintStyle" parent="TextAppearance.Design.Hint"> <item name="fontFamily">@font/iran_sans_medium</item> <item name="android:fontFamily">@font/iran_sans_medium</item> </style> <style name="TextInputLayoutHelperStyle" parent="TextAppearance.Design.HelperText"> <item name="fontFamily">@font/iran_sans_medium</item> <item name="android:fontFamily">@font/iran_sans_medium</item> </style> <style name="TextInputLayoutOutlinedBoxStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox"> <item name="helperTextTextAppearance">@style/TextInputLayoutHelperStyle</item> <item name="errorTextAppearance">@style/TextInputLayoutErrorStyle</item> <item name="hintTextAppearance">@style/TextInputLayoutHintStyle</item> </style>


0
投票

使用可以使用如下所示的style.xml:

样式文件:

<com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_centerInParent="true"
            android:hint="@string/cardname_hint"
            android:layout_marginStart="30dp"
            android:layout_marginEnd="30dp"
            card_view:helperText="@string/cardname_helper"
            style="@style/TextInputLayoutOutlinedBoxStyle"
            android:layout_height="wrap_content">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:fontFamily="@font/iran_sans_medium"
                android:textColor="@color/colorTextPrimary"
                android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

布局文件:

qazxswpoi

19
投票

不幸的是,你必须使用反射来处理这个问题。

浮动标签由CollapsingTextHelper绘制,TypefaceSpan是一个内部的包私有类,并不设置为处理跨度。所以,在这种情况下,使用像自定义final Typeface tf = Typeface.createFromAsset(getAssets(), "your_custom_font.ttf"); final TextInputLayout til = (TextInputLayout) findViewById(R.id.yourTextInputLayout); til.getEditText().setTypeface(tf); try { // Retrieve the CollapsingTextHelper Field final Field cthf = til.getClass().getDeclaredField("mCollapsingTextHelper"); cthf.setAccessible(true); // Retrieve an instance of CollapsingTextHelper and its TextPaint final Object cth = cthf.get(til); final Field tpf = cth.getClass().getDeclaredField("mTextPaint"); tpf.setAccessible(true); // Apply your Typeface to the CollapsingTextHelper TextPaint ((TextPaint) tpf.get(cth)).setTypeface(tf); } catch (Exception ignored) { // Nothing to do } 这样的东西是行不通的。

因为它使用反射,所以不保证将来可以使用。

履行

TextView

错误视图

如果您需要更改错误的字体,可以执行以下两项操作之一:

  1. 使用Reflection抓住错误Typeface并像以前一样应用TextInputLayout
  2. 使用自定义范围。与浮动标签不同,TextView使用的错误视图只是一个final Field errorField = til.getClass().getDeclaredField("mErrorView"); errorField.setAccessible(true); ((TextView) errorField.get(til)).setTypeface(tf); ,因此它能够处理跨度。

用反射

final SpannableString ss = new SpannableString("Error");
ss.setSpan(new FontSpan(tf), 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
til.setError(ss);

private static final class FontSpan extends MetricAffectingSpan {

    private final Typeface mNewFont;

    private FontSpan(Typeface newFont) {
        mNewFont = newFont;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setTypeface(mNewFont);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        paint.setTypeface(mNewFont);
    }

}

使用自定义范围

Smoothie Shoppe

结果

我使用的字体是MaterialComponents


9
投票

我正在使用新的<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> ... <item name="textInputStyle">@style/CustomFontTextInputLayout</item> </style> <!-- region TextInputLayout & TextInputEditText styles --> <style name="TextInputLayout.OutlineBox.CustomFont" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox"> <item name="android:theme">@style/ThemeOverlay.TextInputEditText.OutlinedBox.CustomFont</item> </style> <style name="ThemeOverlay.TextInputEditText.OutlinedBox.CustomFont" parent="ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox"> <item name="editTextStyle">@style/TextInputEditText.OutlinedBox.CustomFont</item> </style> <style name="TextInputEditText.OutlinedBox.CustomFont" parent="Widget.MaterialComponents.TextInputEditText.OutlinedBox"> <item name="android:fontFamily">@font/my_font</item> </style> <style name="CustomFontTextInputLayout" parent="Widget.Design.TextInputLayout"> <item name="hintTextAppearance">@style/TextInputLayoutHintText</item> <item name="helperTextTextAppearance">@style/TextInputLayoutHelperText</item> <item name="errorTextAppearance">@style/TextInputLayoutErrorText</item> </style> <style name="TextInputLayoutHintText" parent="TextAppearance.Design.Hint"> <item name="android:fontFamily">@font/my_font</item> </style> <style name="TextInputLayoutHelperText" parent="TextAppearance.Design.HelperText"> <item name="android:fontFamily">@font/my_font</item> </style> <style name="TextInputLayoutErrorText" parent="TextAppearance.Design.Error"> <item name="android:fontFamily">@font/my_font</item> </style> <!-- endregion --> 主题,没有一个答案帮助了我。

不得不自己玩风格和主题。将发布一大块样式,以防有人面临同样的问题。

<android.support.design.widget.TextInputLayout
    style="@style/TextInputLayout.OutlineBox.CustomFont"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/first_name"/>
</android.support.design.widget.TextInputLayout>

然后在xml布局中:

enter image description here

这是结果:

<android.support.design.widget.TextInputLayout android:id="@+id/text_input1" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/edt_user" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/username"/> </android.support.design.widget.TextInputLayout>


8
投票

我刚刚找到一个简单的解决方案,它对我有用:

通过这种方式,您可以将字体设置为任何编辑文本的提示:

在layout.xml中:

public class MainActivity extends AppCompatActivity {

EditText editText;
TextInputLayout textInputLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Typeface font_yekan= Typeface.createFromAsset(getAssets(), "fonts/byekan.ttf");
        textInputLayout= (TextInputLayout) findViewById(R.id.text_input1);
    textInputLayout.setTypeface(font_yekan);
      }
 }

在java类中:

adneal's answer

6
投票

这是public class CustomTextInputLayout extends TextInputLayout { public CustomTextInputLayout(Context context) { super(context); initFont(context); } public CustomTextInputLayout(Context context, AttributeSet attrs) { super(context, attrs); initFont(context); } private void initFont(Context context) { final Typeface typeface = Typeface.createFromAsset( context.getAssets(), "fonts/YOUR_CUSTOM_FONT.ttf"); EditText editText = getEditText(); if (editText != null) { editText.setTypeface(typeface); } try { // Retrieve the CollapsingTextHelper Field final Field cthf = TextInputLayout.class.getDeclaredField("mCollapsingTextHelper"); cthf.setAccessible(true); // Retrieve an instance of CollapsingTextHelper and its TextPaint final Object cth = cthf.get(this); final Field tpf = cth.getClass().getDeclaredField("mTextPaint"); tpf.setAccessible(true); // Apply your Typeface to the CollapsingTextHelper TextPaint ((TextPaint) tpf.get(cth)).setTypeface(typeface); } catch (Exception ignored) { // Nothing to do } } } 的自定义类实现。

CustomTextInputLayout

在您的XML文件中,您现在需要使用TextInputLayout而不是<your.package.CustomTextInputLayout android:id="@+id/textInputLayout_email" android:layout_width="match_parent" android:layout_height="wrap_content" > <AutoCompleteTextView android:id="@+id/editText_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/hint_email" android:inputType="textEmailAddress" /> ,它将开箱即用。

adneal

谢谢final Typeface tf = Typeface.createFromAsset(getAssets(), "your_custom_font.ttf"); final TextInputLayout til = (TextInputLayout) findViewById(R.id.yourTextInputLayout); til.getEditText().setTypeface(tf); til.setTypeface(tf); 的答案。


3
投票
@Override
public void setErrorEnabled(boolean enabled) {

    super.setErrorEnabled(enabled);

    if (enabled) {

        try {

            Field cthf = TextInputLayout.class.getDeclaredField("mErrorView");
            cthf.setAccessible(true);

            TextView error = (TextView) cthf.get(this);

            if (error != null)
                error.setTypeface(tf);


        } catch (Exception e) {

        }
    }
}

1
投票

解决@adneal中的问题答案:如果setErrorEnabled未设置为true,则mErrorView将为null,如果在任何时候将其设置为false,则字体将更改回默认值。所以解决它:

在您自定义TextInputLayout覆盖setErrorEnabled

<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
        <item name="android:fontFamily">@font/poppins_regular</item>
    </style>

1
投票

有一种更简单的方法,

在名为“font”的“res”文件夹中创建一个新目录,并在其中放置一个字体。然后打开“样式”文件并创建一个新样式:

<android.support.design.widget.TextInputLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:hintTextAppearance="@style/customfontstyle"
       >

        <android.support.design.widget.TextInputEditText
            android:layout_width="220dp"
            android:layout_height="wrap_content"
            android:id="@+id/edit_phone_number"
            android:hint="@string/phone_number_label"

            android:inputType="number"
            />
    </android.support.design.widget.TextInputLayout>

您还可以添加更多属性,例如textColor,textSize等。

然后在你的XML中:

edit_login_emailOrPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus)
            {
                textInputLayout_login_emailOrPhone.setTypeface(APSApplication.getInstance().getFonts().getTypefaceSemiBold());
            }else
            {
                textInputLayout_login_emailOrPhone.setTypeface(APSApplication.getInstance().getFonts().getTypefaceRegular());
            }
        }
    });

我检查了它,它的工作原理。


0
投票

这就是我实现这一目标的方式

Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
© www.soinside.com 2019 - 2024. All rights reserved.