设置默认值(未聚焦)TextInputLayout提示textSize

时间:2019-08-01 21:52:52

标签: android material-design

我正在尝试以编程方式更改提示文本的大小,但我只是找不到正确的方法。我正在使用setHintTextAppearance,如示例所示,但是它仅在输入集中或填充了一些数据时才起作用。我也尝试设置EditText textSize,但还是没有运气。

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

3 个答案:

答案 0 :(得分:2)

您可以使用这样的反射来更改提示文字不集中时的大小;

 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();
    }
根据TextInputLayout的依赖版本,mExpandedTextSize的

名称可能有所不同。您应该检查TextInputLayout和CollapsingTextHelper类中的变量名称。

希望这对您有所帮助。

答案 1 :(得分:1)

反射解决方案不适用于support:design:28.0.0(mExpandedTextSize-> expandedTextSize)。另外,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);
    }
}

答案 2 :(得分:0)

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

==26339==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 144 byte(s) in 1 object(s) allocated from:
    #0 0x7f46fad68510 in malloc (/usr/lib64/libasan.so.4+0xdc510)
    #1 0x407754 in __sectiontest_MOD_constructor /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe_alternative/recursive_mwe.f90:27
    #2 0x403939 in __sectiontest_MOD_demo /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe_alternative/recursive_mwe.f90:95
    #3 0x408564 in MAIN__ /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe_alternative/recursive_mwe_prog.f90:5
    #4 0x4085a4 in main /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe_alternative/recursive_mwe_prog.f90:2
    #5 0x7f46f9d8ef89 in __libc_start_main (/lib64/libc.so.6+0x20f89)

Direct leak of 96 byte(s) in 1 object(s) allocated from:
    #0 0x7f46fad68510 in malloc (/usr/lib64/libasan.so.4+0xdc510)
    #1 0x407754 in __sectiontest_MOD_constructor /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe_alternative/recursive_mwe.f90:27
    #2 0x403d72 in __sectiontest_MOD_demo /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe_alternative/recursive_mwe.f90:95
    #3 0x408564 in MAIN__ /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe_alternative/recursive_mwe_prog.f90:5
    #4 0x4085a4 in main /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe_alternative/recursive_mwe_prog.f90:2
    #5 0x7f46f9d8ef89 in __libc_start_main (/lib64/libc.so.6+0x20f89)

Indirect leak of 144 byte(s) in 1 object(s) allocated from:
    #0 0x7f46fad68510 in malloc (/usr/lib64/libasan.so.4+0xdc510)
    #1 0x405fec in __sectiontest_MOD_section_assign /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe_alternative/recursive_mwe.f90:50
    #2 0x408237 in __sectiontest_MOD_constructor /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe_alternative/recursive_mwe.f90:30
    #3 0x403d72 in __sectiontest_MOD_demo /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe_alternative/recursive_mwe.f90:95
    #4 0x408564 in MAIN__ /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe_alternative/recursive_mwe_prog.f90:5
    #5 0x4085a4 in main /users/tiziano/work/tests/fortran/cp2k_input_parser/recursive_mwe_alternative/recursive_mwe_prog.f90:2
    #6 0x7f46f9d8ef89 in __libc_start_main (/lib64/libc.so.6+0x20f89)

SUMMARY: AddressSanitizer: 384 byte(s) leaked in 3 allocation(s).

如果需要编程,则可以按id查找edittext并设置文本大小。