Изменить шрифт плавающей метки EditText и TextInputLayout

Кто-нибудь пробовал менять шрифт плавающей метки? Я изменил исходник 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)


Начиная с Design Library v23 вы можете использовать TextInputLayout#setTypeface().

Это установит шрифт как для расширенной, так и для плавающей подсказки.

Вот запрос функции, где он обсуждался на b.android.com.

EDIT: Шрифт представления ошибки не устанавливался, но теперь исправлено в v25.1.0.

person Austyn Mahoney    schedule 02.11.2015
comment
Я сделал это, и это сработало для всего, кроме просмотра ошибок. Я думаю, что внешний вид текста перезаписывает его, так что, вероятно, это какая-то ошибка. Чтобы настроить конкретное представление ошибок, вы можете использовать предоставленный Android идентификатор для ссылки на него следующим образом: ((TextView)inputLayout.findViewById(R.id.textinput_error)).setTypeface(customFont); - person Justin Liu; 27.01.2017
comment
@JustinLiu Ваша проблема была исправлена ​​в библиотеке поддержки версии 25.1.0 - code.google .com/p/android/issues/detail?id=227803 - person Austyn Mahoney; 31.01.2017

К сожалению, вам придется использовать отражение, чтобы справиться с этим.

Плавающая метка рисуется 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. Используйте Reflection, возьмите ошибку TextView и примените Typeface, как и раньше.
  2. Используйте пользовательский диапазон. В отличие от плавающей метки, представление ошибок, используемое TextInputLayout, представляет собой просто 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.

person adneal    schedule 10.06.2015
comment
Как я могу использовать это в одном классе, моя проблема в том, что у меня так много макетов, что мне все время приходится использовать этот код во всех классах, поэтому я и спрашиваю. Возможно ли это? - person Aman Jham; 06.08.2015
comment
Ужасно, что вам приходится делать это таким образом, но пока это сработает. Однако ((TextView) errorField.get(til)) возвращает для меня значение null :( - person Tobbbe; 24.09.2015
comment
Кажется, теперь вы можете использовать setTypeface(Typeface typeface) в TextInputLayout. :) - person StingRay5; 27.10.2015
comment
Я хочу изменить только шрифт с плавающей меткой. Является ли это возможным? - person LMaker; 25.08.2020

Я использую новый 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>

Вот результат:

введите описание изображения здесь

person azizbekian    schedule 03.09.2018

я только что нашел простое решение, и оно сработало для меня:

таким образом вы можете настроить шрифт так, чтобы он намекал на любой редактируемый текст:

в 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);
      }
 }
person imansdn    schedule 23.05.2016
comment
Странно, это должно было сработать для меня. Я использовал собственный макет ввода текста и изменил шрифт только при инициализации с помощью setTypeface(), и все же представление ошибок имеет более старый вариант. Это странно, так как в коде представление ошибок также принимает шрифт в setTypeface(). Любая подсказка, что я могу упустить? - person AA_PV; 02.01.2017

Вот реализация пользовательского класса для ответа 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 за ответ.

person Aleksandar Ilic    schedule 13.08.2015
comment
Не работает с com.google.android.material.textfield.TextInputLayout - person Arslan Tazhibaev; 20.08.2020

Есть более простой способ,

Создайте новый каталог в папке «res» с именем «font» и поместите туда шрифт. Затем откройте файл стилей и создайте новый стиль:

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

Я проверил это, и это работает.

person Danish Ajaib    schedule 03.07.2018

Я искал это, я нашел этот способ, используя библиотеку поддержки:

Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);

и установите этот шрифт на свой TextInpuLayout.

Для меня работает как шарм, я надеюсь, что это поможет другим =]

Источник: документация

person Lucas Orso    schedule 25.07.2018

Использование может использовать 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>
person Ehsan Mashhadi    schedule 07.11.2018
comment
спасибо, это сработало для меня. - person The MJ; 12.04.2021

исправление проблемы в ответе @adneal: если для setErrorEnabled не установлено значение true, mErrorView будет иметь значение null, и если вы установите значение false в любой момент, шрифт вернется к значению по умолчанию. поэтому, чтобы исправить это:

в вашем пользовательском TextInputLayout переопределить setErrorEnabled

@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) {

        }
    }
}
person Ashkan Ghodrat    schedule 30.12.2015

Вот как я этого добиваюсь

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());
            }
        }
    });
person zohaib khaliq    schedule 13.09.2017

Если вы тоже выполнили экзотическое требование установить пользовательский шрифт ТОЛЬКО для плавающей метки, и что-то еще не сработало для вас, попробуйте это. Это сработало для меня, по крайней мере, для материальной библиотеки версии. 1.3.0-альфа03.

@SuppressLint("RestrictedApi")
fun setHintFontFamily(view: TextInputLayout, fontRes: Int) {
    val font = ResourcesCompat.getFont(view.context, fontRes)!!

    try {
        val collapsingTextHelperField =
            view::class.java.getDeclaredField("collapsingTextHelper").apply {
                isAccessible = true
            }
        val collapsingTextHelper = collapsingTextHelperField.get(view) as CollapsingTextHelper

        collapsingTextHelper.collapsedTypeface = font
    } catch (e: Exception) {
    }
}

Сначала мы получаем CollapsingTextHelper, как и в некоторых других ответах, но затем мы используем его свойство collapsedTypeface, которое, похоже, делает именно то, что нам нужно — применять шрифт только к плавающей метке. Обратите внимание, что видимость этого свойства ограничена группой библиотек (поэтому я использовал @SuppressLint). Таким образом, детали реализации могут измениться в будущем.

person Isao Nokarikashi    schedule 23.11.2020

person    schedule
comment
Пожалуйста, объясните что-нибудь о вашем подходе - person etalon11; 30.01.2016
comment
@ etalon11, текстовое представление или текст редактирования будут заключены в TextInputLayout, и применение шрифта для TextInputLayout в конечном итоге применит атрибуты к его дочерним элементам. Я верю, что это сработает. - person Satty; 12.08.2016
comment
Член TextInputLayout mEditText, который возвращает getEditText(), отличается от mErrorView, который является TextView. Это не должно работать, пожалуйста, проверьте. - person AA_PV; 02.01.2017