【问题标题】:Set default(unfocused) TextInputLayout hint textSize设置默认(未聚焦) TextInputLayout 提示 textSize
【发布时间】:2019-08-01 21:52:52
【问题描述】:

我正在尝试以编程方式更改提示文本大小,但我找不到正确的方法。我正在使用 setHintTextAppearance,就像它在示例中显示的那样,但它仅在输入聚焦或填充一些数据时才有效。我也尝试设置 EditText textSize,但仍然没有运气。

textInputLayout.setHintTextAppearance(Vabaco_TextInputLayout_hint_small);
EditText a = textInputLayout.getEditText();
a.setTextSize(8);

【问题讨论】:

    标签: android material-design


    【解决方案1】:

    当提示文本不聚焦时,您可以使用这样的反射来更改提示文本大小;

     try {
            Field filed = TextInputLayout.class.getDeclaredField("mCollapsingTextHelper");
            filed.setAccessible(true);
            Object helper = filed.get(textInputLayout);
    
            Field f1 = helper.getClass().getDeclaredField("mExpandedTextSize");
            f1.setAccessible(true);
    
            f1.set(helper,100);
        }
        catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    

    mExpandedTextSize 的名称可能会根据 TextInputLayout 的依赖版本而有所不同。您应该检查 TextInputLayout 和 CollapsingTextHelper 类的变量名称。

    希望对你有所帮助。

    【讨论】:

    • 它确实有效!谢谢。唯一的问题是,f1.set(),第二个参数是用户百分比吗?因为肯定不是积分。抱歉,回复晚了。工作很辛苦。
    • 其实我给了一个随机数,但是你可以给这个大小,比如this@LeoOdishvili
    【解决方案2】:

    反射解决方案在 support:design:28.0.0(mExpandedTextSize->expandTextSize) 上不起作用。此外,Android Q(及更高版本)不支持某些非 SDK 解决方案。

    创建您的自定义布局:

    public  class CustomTextInputLayout extends TextInputLayout {
        public CustomTextInputLayout(Context context) {
            super(context);
        }
    
        @Override
        public void addView(View child, int index, ViewGroup.LayoutParams params) {
            if(child instanceof EditText) {
                ((EditText)child).setTextSize(16);
            }
    
            super.addView(child, index, params);
        }
    }
    

    【讨论】:

    • 谢谢!我会在业余时间尝试这样做。
    【解决方案3】:

    如果不需要以编程方式设置文本大小,您可以尝试如下,我已禁用 TextInputLayout 提示,

    <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:hintEnabled="false">
    
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/edittext"
                    android:hint="yorhint"
                    android:inputType="text"
                    android:textColorHint="@color/colorLightBlack"
                    android:textSize="10sp" />
            </android.support.design.widget.TextInputLayout>
    
    

    如果需要以编程方式,您可以通过 id 找到 edittext 并设置文本大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-14
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 2022-01-21
      • 2018-02-17
      • 1970-01-01
      • 2011-10-15
      相关资源
      最近更新 更多