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

Che*_*eng 1 android android-fonts

目前,我希望对的浮动文本产生大胆的影响InputTextLayout。这就是我在做什么

this.usernameTextInputLayout.setTypeface(Utils.ROBOTO_BOLD_TYPE_FACE);
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

但是,这将给我带来另一种不良影响。提示文本也将变为粗体。

在此处输入图片说明

您可以比较上面的两个图像。请注意,出于比较目的,我照passwordTextInputLayout原样离开。

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

Mik*_* M. 5

如您所知,TextInputLayout使用私有帮助器类来处理提示文本样式和动画。此类- 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有些违反直觉,TextInputLayout折叠状态是当提示高于浮置标签EditText。它的展开状态是当提示位于中的“正常”位置时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>
Run Code Online (Sandbox Code Playgroud)

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

CustomTextInputLayout usernameTextInputLayout =
    (CustomTextInputLayout) findViewById(R.id.username_til);

usernameTextInputLayout.setCollapsedTypeface(Utils.ROBOTO_BOLD_TYPE_FACE);
Run Code Online (Sandbox Code Playgroud)

CollapsingTextHelper在支持库的23.1.0版本中添加了上面使用的方法。如果您使用的是以前的版本,或者NoSuchMethodException由于其他原因而获得,无论版本如何,直接设置字体字段的答案的原始版本都可以使用。

  • @UsmanRana 在`onFinishInflate()` 中进行。到那时应该完成设置。 (2认同)