Android 更改浮动标签EditText和TextInputLayout的字体
有人试图更改浮动标签的字体?我改变了EditText的来源,但是浮动标签的字体没有改变,我非常感谢那些帮助我的人 代码:Android 更改浮动标签EditText和TextInputLayout的字体,android,android-edittext,android-design-library,android-textinputlayout,material-components,Android,Android Edittext,Android Design Library,Android Textinputlayout,Material Components,有人试图更改浮动标签的字体?我改变了EditText的来源,但是浮动标签的字体没有改变,我非常感谢那些帮助我的人 代码: ----------------- etTextoDescricao=(EditText)findViewById(R.id.etTextoDescricao); etTextoDescricao.setTypeface(CustomTypeface.getTypefaceMediumDefault(this)); 不幸的是,您必须使用反射来处理此问题 浮动标签由col
-----------------
etTextoDescricao=(EditText)findViewById(R.id.etTextoDescricao);
etTextoDescricao.setTypeface(CustomTypeface.getTypefaceMediumDefault(this));
不幸的是,您必须使用反射来处理此问题 浮动标签由
collasingTextHelper
绘制,它是一个内部的包私有类,未设置为处理跨度。因此,在这种情况下,使用自定义字体span
之类的东西是行不通的
因为它使用反射,所以不能保证将来会工作
实施
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
}
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);
}
}
错误视图
如果需要更改错误的字体,可以执行以下两种操作之一:
文本视图
,并像以前一样应用字体
TextInputLayout
使用的错误视图只是一个TextView
,因此它能够处理跨度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
}
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);
}
}
使用自定义范围
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
}
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);
}
}
结果
我使用的字体是。这里是一个自定义的类实现 现在,在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" />
关于答案。从设计库v23开始,您可以使用
这将在扩展提示和浮动提示上设置字体
这里是讨论它的地方
编辑:未设置错误视图字体,但现在处于v25.1.0
修复@adneal answer:
如果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) {
}
}
}
我刚刚找到了一个简单的解决方案,它对我很有效:
通过这种方式,您可以将字体设置为任何编辑文本的提示:
在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>
我就是这样做到的
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());
}
}
});
有一个更简单的方法
在“res”文件夹中创建一个名为“font”的新目录,并在其中放置字体。然后打开“样式”文件并创建新样式:
<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
<item name="android:fontFamily">@font/poppins_regular</item>
</style>
@字体/poppins\u常规
您还可以添加更多属性,例如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>
我检查了它,它正常工作。我正在寻找这个,我通过使用支持库找到了这个方法:
Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
并将此字体设置为yout TextInpuLayout
对我来说,它就像魅力一样,我希望它能帮助其他人=]
来源:我正在使用新主题,但没有一个答案对我有帮助
我必须自己处理风格和主题。如果有人面临同样的问题,我会在这里发布一大块样式
<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 -->
...
@样式/自定义FontTextInputLayout
@style/ThemeOverlay.textinputtext.OutlinedBox.CustomFont
@样式/text输入文字text.OutlinedBox.CustomFont
@字体/我的
@样式/文本输入布局输出文本
@样式/文本InputLayoutUserPerText
@样式/文本InputLayoutRorText
@字体/我的
@字体/我的
@字体/我的
然后在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>
使用可以使用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>
@font/iran_sans_中等
@font/iran_sans_中等
@font/iran_sans_中等
@font/iran_sans_中等
@font/iran_sans_中等
@font/iran_sans_中等
@样式/文本InputLayoutUserPerstyle
@样式/文本InputLayoutRorStyle
@样式/文本输入布局输出样式
布局文件:
<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.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) {
}
}
首先,我们得到了与其他答案一样的collasingTextHelper
,但随后我们使用了它的属性collapsedTypeface
,这似乎正是我们所需要的——只对浮动标签应用字体。请注意,此属性的可见性仅限于库组(这就是为什么我使用@SuppressLint
)。因此,未来实现细节可能会发生变化。我如何在单个类中使用此代码,我的问题是我有这么多布局,然后一直在所有类中使用此代码,这就是我为什么要问的原因。有可能吗?很糟糕,你必须这样做,但现在可以了。但是,((TextView)errorField.get(til))为我返回null:(您现在似乎可以在TextInputLayout上使用setTypeface(Typeface-Typeface)
。:)我只想更改浮动标签字体。可能吗?请解释一下你的想法approach@etalon11,文本视图或编辑文本将被包装在文本输入布局
中,为文本输入布局
应用类型面将最终将属性应用于其子项。getEditText()返回的TextInputLayout成员mEditText与mErrorView不同,mErrorView是一个TextView。这不应该起作用,请检查。奇怪,这应该对我起作用。我使用了自定义文本输入布局,仅在使用setTypeface()初始化时更改了字体,错误视图仍然使用旧的字体。这很奇怪,因为在代码中,错误视图在setTypeface()中也采用typeface。有什么线索我可能会错过吗?我这样做了,它的一切工作,但错误的看法。我认为文本外观覆盖了它,所以这可能是某种错误。瞄准