Изменить шрифт плавающей метки 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));
![enter image description here]()
Ответы
Ответ 1
Как и в Design Library v23
, вы можете использовать TextInputLayout#setTypeface()
.
Это установит шрифт как для расширенного, так и для плавающего подсказки.
Вот запрос функции, где он обсуждался на b.android.com.
EDIT: Тип шрифта с ошибкой не был установлен, но теперь исправлено в v25.1.0
.
Ответ 2
К сожалению, вам придется использовать отражение, чтобы справиться с этим.
Плавающая метка нарисована 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
}
Просмотр ошибок
Если вам нужно изменить шрифт ошибки, вы можете сделать одну из двух вещей:
- Использовать Reflection захватить ошибку
TextView
и применить Typeface
так же, как раньше
- Используйте настраиваемый диапазон. В отличие от плавающей метки, представление ошибки, используемое
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);
}
}
Результаты
![results]()
Шрифт, который я использую, Smoothie Shoppe.
Ответ 3
Вот специальная реализация класса для ответа 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 за ответ.
Ответ 4
Я просто нашел простое решение, и это сработало для меня:
таким образом вы можете установить шрифт для подсказки любого текста редактирования:
в 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);
}
}
Ответ 5
исправление проблемы в ответе @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) {
}
}
}
Ответ 6
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);
Ответ 7
Вот как я достигаю этого
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());
}
}
});