Мне повезло с trickle . Это классно, потому что он может дросселировать произвольные пользовательские приложения без изменений. Он работает, предварительно загружая собственные функции оболочки send / recv, которые делают для вас расчет пропускной способности.
Самый большой недостаток, который я нашел, заключался в том, что сложно согласовать несколько приложений, которые вы хотите использовать для конечной полосы пропускания. «trickled» помогает, но мне было сложно.
Обновление в 2017 году: похоже, что тряска переместилась на https://github.com/mariusae/trickle
К сожалению, вам придется использовать отражение, чтобы справиться с этим.
Плавающая метка рисуется 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
и примените 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);
}
}
Результаты
Я использую шрифт Smoothie Shoppe .
Устранение проблемы в ответе @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) {
}
}
}
Вот как я этого добиваюсь
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());
}
}
});
Использование можно использовать 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>
Вот пользовательская реализация класса для ответа Аднеала .
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 за ответ.