android - 是否可以为 InputTextLayout 的 float 文本和提示文本设置不同的字体

标签 android android-fonts

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

this.usernameTextInputLayout.setTypeface(Utils.ROBOTO_BOLD_TYPE_FACE);

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

enter image description here

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

enter image description here

您可以比较以上两张图片。请注意,为了比较,我将 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 版本中添加的。如果您使用的是以前的版本,或者由于某些其他原因收到 NoSuchMethodExceptionthe original version of my answer无论版本如何,直接设置字体字段都应该有效。

关于android - 是否可以为 InputTextLayout 的 float 文本和提示文本设置不同的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40201038/

相关文章:

Android:获取MapView传输的字节数

Android - 如何使用包含多种字体的自定义字体文件

android - 在 Android 中为不同的语言使用不同的字体

android - 从广告点击返回后 Activity 重新启动

android - CoronaSDK 屏幕上同时出现两个场景

android - Shared Pref 在某些高端设备(如三星)中返回错误值

android - 根据当前的构建/ flavor 设置设置一个值

android - 更改详细信息 fragment 上列表 fragment 的字体

android - 如何将整个应用程序的可下载字体设置为默认字体?

android - 如何在android中使用不同的文本大小集?