目前,我想对我的 InputTextLayout
float 文本进行加粗效果。这是我正在做的
this.usernameTextInputLayout.setTypeface(Utils.ROBOTO_BOLD_TYPE_FACE);
它按预期工作。 float 文本(用户名)已变为粗体。
但是,这会对我造成另一个不良影响。提示文本也将变为粗体。
您可以比较以上两张图片。请注意,为了比较,我将 passwordTextInputLayout
保持原样。
InputTextLayout
的 float 文本和提示文本是否可以有不同的字体?
最佳答案
如您所知,TextInputLayout
使用私有(private)助手类来处理提示文本样式和动画。此类 - CollapsingTextHelper
- 为其折叠 和展开 状态维护单独的字体。我们只需要设置正确的,我们将使用反射来完成。
我通常将这些类型的功能打包到自定义子类中,所以我也会在这里做同样的事情。如果您不想使用子类,可以轻松地将反射内容放入您可以放入 Activity
或实用程序类中的一些简单方法中。
public class CustomTextInputLayout extends TextInputLayout {
private Object collapsingTextHelper;
private Method setCollapsedTypefaceMethod;
private Method setExpandedTypefaceMethod;
public CustomTextInputLayout(Context context) {
this(context, null);
}
public CustomTextInputLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
try {
Field cthField = TextInputLayout.class
.getDeclaredField("mCollapsingTextHelper");
cthField.setAccessible(true);
collapsingTextHelper = cthField.get(this);
setCollapsedTypefaceMethod = collapsingTextHelper
.getClass().getDeclaredMethod("setCollapsedTypeface", Typeface.class);
setCollapsedTypefaceMethod.setAccessible(true);
setExpandedTypefaceMethod = collapsingTextHelper
.getClass().getDeclaredMethod("setExpandedTypeface", Typeface.class);
setExpandedTypefaceMethod.setAccessible(true);
}
catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) {
collapsingTextHelper = null;
setCollapsedTypefaceMethod = null;
setExpandedTypefaceMethod = null;
e.printStackTrace();
}
}
public void setCollapsedTypeface(Typeface typeface) {
if (collapsingTextHelper == null) {
return;
}
try {
setCollapsedTypefaceMethod.invoke(collapsingTextHelper, typeface);
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
public void setExpandedTypeface(Typeface typeface) {
if (collapsingTextHelper == null) {
return;
}
try {
setExpandedTypefaceMethod.invoke(collapsingTextHelper, typeface);
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
有点违反直觉,TextInputLayout
的折叠 状态是当提示是 EditText
上方的 float 标签时。它的扩展 状态是当提示处于EditText
内的“正常”位置时。上面给出了为两种状态设置字体的方法。
这是 TextInputLayout
的直接替代品,您可以像往常一样在布局中使用它。例如:
<com.mycompany.myapp.CustomTextInputLayout
android:id="@+id/username_til"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/TextLabel">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:hint="Username" />
</com.mycompany.myapp.CustomTextInputLayout>
在您的代码中,设置 float 文本提示的字体:
CustomTextInputLayout usernameTextInputLayout =
(CustomTextInputLayout) findViewById(R.id.username_til);
usernameTextInputLayout.setCollapsedTypeface(Utils.ROBOTO_BOLD_TYPE_FACE);
上面使用的 CollapsingTextHelper
方法是在支持库的 23.1.0 版本中添加的。如果您使用的是以前的版本,或者由于某些其他原因收到 NoSuchMethodException
,the original version of my answer无论版本如何,直接设置字体字段都应该有效。
关于android - 是否可以为 InputTextLayout 的 float 文本和提示文本设置不同的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40201038/