Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

android - How to change the size of the EditText hint in TextInputLayout

I'm trying to change the hint size in TextInputLayout, but it's not working as desired. This is what I'd like to achieve:

Desired result


styles.xml

<style name="TextLabel" parent="TextAppearance.Design.Hint">
    <item name="android:textSize">44sp</item>
</style>

fragment.xml

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintTextAppearance="@style/TextLabel"
    android:hint="Password">

    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"/>

</android.support.design.widget.TextInputLayout>

This code works only for the floating label, when the EditText isn't empty, but I want to change the hint size in the EditText itself, when it is empty.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The size of the regular hint text is set to the EditText's text size when that is added to the TextInputLayout during inflation/initialization. This value is ultimately set on a private helper class in TextInputLayout, and there is no publicly exposed method or field to change it.

However, we can do a little juggling with the text sizes by subclassing TextInputLayout to intercept the adding of the EditText. When the EditText is added, we cache its text size, set the desired hint size as its text size, allow the super class to add it and initialize the hint, and finally set the EditText's text size back to its original value.

For example:

public class CustomTextInputLayout extends TextInputLayout {

    private float mainHintTextSize;
    private float editTextSize;

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

        TypedArray a = context.obtainStyledAttributes(
            attrs, R.styleable.CustomTextInputLayout);

        mainHintTextSize = a.getDimensionPixelSize(
            R.styleable.CustomTextInputLayout_mainHintTextSize, 0);

        a.recycle();
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        final boolean b = child instanceof EditText && mainHintTextSize > 0;

        if (b) {
            final EditText e = (EditText) child;
            editTextSize = e.getTextSize();
            e.setTextSize(TypedValue.COMPLEX_UNIT_PX, mainHintTextSize);
        }

        super.addView(child, index, params);

        if (b) {
            getEditText().setTextSize(TypedValue.COMPLEX_UNIT_PX, editTextSize);
        }
    }

    // Units are pixels.

    public float getMainHintTextSize() {
        return mainHintTextSize;
    }

    // This optional method allows for dynamic instantiation of this class and
    // its EditText, but it cannot be used after the EditText has been added.
    // Units are scaled pixels.

    public void setMainHintTextSize(float size) {
        if (getEditText() != null) {
            throw new IllegalStateException(
                "Hint text size must be set before EditText is added");
        }

        mainHintTextSize = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP, size, getResources().getDisplayMetrics());
    }
}

To use the custom mainHintTextSize attribute, we'll need the following in our <resources>, which we can do by just sticking the following file in the res/values/ folder, or adding to the one that's already there.

attrs.xml

<resources>
    <declare-styleable name="CustomTextInputLayout" >
        <attr name="mainHintTextSize" format="dimension" />
    </declare-styleable>
</resources>

If you don't care to use the custom attribute, you can skip this file, and remove the TypedArray processing in the third constructor above.

This custom class is a drop-in replacement for TextInputLayout, and can be used just as it would. For example:

<com.mycompany.myapp.CustomTextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Password"
    app:hintTextAppearance="@style/TextLabel"
    app:mainHintTextSize="12sp">

    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:text="qwerty123" />

</com.mycompany.myapp.CustomTextInputLayout>

screenshot


This approach is nice, in that it uses only publicly-accessible, documented methods, but the hint text size must be set before the EditText is added, whether that happens during inflation, or through direct instantiation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...