是否可以为 InputTextLayout 的浮动文本和提示文本设置不同的字体

Is it possible to have different font for floating text and hint text of InputTextLayout

目前,我想对我的 InputTextLayout 的浮动文本进行加粗效果。这就是我正在做的

this.usernameTextInputLayout.setTypeface(Utils.ROBOTO_BOLD_TYPE_FACE);

它按预期工作。浮动文本(用户名)已变为粗体。

但是,这将对我产生另一个不良影响。提示文字也会变成粗体。

您可以比较以上2张图片。请注意,为了便于比较,我将 passwordTextInputLayout 保持原样。

InputTextLayout的浮动文本和提示文本是否可以有不同的字体?

如您所知,TextInputLayout 使用私有助手 class 来处理提示文本样式和动画。 class - CollapsingTextHelper - 为其 collapsedexpanded 状态维护单独的字体。我们只需要设置正确的,我们将使用反射来完成。

我通常将这些类型的功能打包到自定义子class中,所以我将在这里做同样的事情。如果你不想使用 subclass,反射的东西可以很容易地放入一些简单的方法中,你可以将它们放入你的 Activity 或实用程序 class.

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();
        }
    }
}

有点违反直觉,TextInputLayoutcollapsed 状态是当提示是 EditText 上方的浮动标签时。它的 expanded 状态是提示位于 "normal" 位置,在 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>

在您的代码中,设置浮动文本提示的字体:

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 应该可以工作,无论版本如何。