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

34

有人尝试更改浮动标签的字体吗?我已经更改了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));

输入图像描述

12个回答

36

4
除了错误视图之外,我已经尝试过这个方法并且运作良好。我认为是由于文本外观被覆盖导致的,这可能是某种错误。要针对特定的错误视图,您可以使用Android提供的ID来引用它,像这样:**((TextView)inputLayout.findViewById(R.id.textinput_error)).setTypeface(customFont);** - Justin Liu
@JustinLiu 您的问题已在Support Library v25.1.0中得到解决 - https://code.google.com/p/android/issues/detail?id=227803 - Austyn Mahoney

21

很遗憾,你需要使用反射来处理这个问题。

浮动标签是由 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
}

错误视图

如果你需要更改错误的字体,你可以采取以下两种方式之一:

  1. 使用反射来获取错误的 TextView 并像以前一样应用 Typeface
  2. 使用自定义 span。与浮动标签不同,由 TextInputLayout 使用的错误视图只是一个 TextView,因此它可以处理 spans。

使用反射

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

}

结果

results

我正在使用的字体是Smoothie Shoppe


我该如何在单个类中使用它?我的问题是,我有很多布局,所以每次都必须在所有类中使用这段代码,这就是我在问的原因。这可能吗? - Aman Jham
很遗憾你必须这样做,但现在它能够工作。 然而,((TextView) errorField.get(til)) 对我来说返回 null :( - Tobbbe
7
看起来现在可以在TextInputLayout上使用setTypeface(Typeface typeface)了。 :) - StingRay5
2
我想仅更改浮动标签字体,这可行吗? - LMaker

21

我正在使用新的MaterialComponents主题,但是之前的答案都没有帮助到我。

我不得不自己调试样式和主题。为了帮助其他人面对同样的问题,我会在这里发布一大段样式代码。

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

然后在xml布局中:

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

这是结果:

输入图片描述


在密码字段中使用提示对我不起作用,因为此处描述的问题。必须在代码中设置字体。 - Vadim Kotov
它可以改变字体,但表单框的样式消失了。 - aijogja

9

我刚刚找到了一个简单的解决方案,对我起作用:

你可以通过以下方式将任何编辑文本的字体类型设置为提示:

在 layout.xml 中:

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

在Java类中:
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);
      }
 }

2
奇怪,这应该对我起作用。我使用了自定义文本输入布局,并在初始化时仅更改了字体类型,使用setTypeface(),但错误视图仍然具有旧的字体。这很奇怪,因为代码中也指定了错误视图采用setTypeface()设置字体。你有什么线索吗?我可能错过了什么吗? - AA_PV

7
这是一个针对adneal的答案的自定义类实现。
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
        }
    }
}

现在您的XML文件中需要使用CustomTextInputLayout代替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的答案。


没有使用过 com.google.android.material.textfield.TextInputLayout。 - Arslan Tazhibaev

5
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);

请解释一下你的方法。 - etalon11
1
@etalon11,一个TextView或EditText将被包裹在TextInputLayout中,并为TextInputLayout应用字体类型,最终将应用属性到其子元素。我相信这会起作用。 - Satty
TextInputLayout成员mEditText,其getEditText()返回的对象与mErrorView不同,后者是一个TextView。这应该是不正常的,请检查一下。 - AA_PV

4

有一个更简单的方法,

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

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

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

然后在您的XML中:

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

我检查了它,它可以正常工作。


3
您可以像下面这样使用style.xml:
样式文件:
<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>

布局文件:

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

1
我正在寻找这个,我发现这种方法,使用支持库:
Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);

并将此字体设置为您的TextInpuLayout。

对我来说,这很有用,我希望能帮助其他人 =]

来源:文档


0

这是我实现它的方式

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

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