簡體   English   中英

如何在TextInputLayout中更改EditText提示的大小

[英]How to change the size of the EditText hint in TextInputLayout

我正在嘗試更改TextInputLayout的提示大小,但無法按預期工作。 這是我想要實現的:

所需結果


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>

EditText不為空時,此代碼僅適用於浮動標簽,但是當它為空時,我想更改EditText本身中的提示大小。

將常規提示文本的大小設置為EditText的文本大小,當在膨脹/初始化期間將其添加到TextInputLayout時。 該值最終在TextInputLayout的私有幫助器類上設置,並且沒有公開暴露的方法或字段可以對其進行更改。

但是,我們可以通過將TextInputLayout子類TextInputLayout子類來攔截EditText的添加,從而對文本大小進行一些調整。 添加EditText ,我們緩存其文本大小,將所需的提示大小設置為其文本大小,允許超類添加它並初始化提示,最后將EditText的文本大小設置回其原始值。

例如:

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

要使用自定義的mainHintTextSize屬性,我們需要在<resources>中執行以下操作,只需將以下文件粘貼到res/values/文件夾中,或添加到已有的文件中即可。

attrs.xml

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

如果您不想使用custom屬性,則可以跳過此文件,並在上面的第三個構造函數中刪除TypedArray處理。

這個自定義類可以替代TextInputLayout ,並且可以像使用它一樣使用。 例如:

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

屏幕截圖


這種方法很不錯,因為它僅使用可公開訪問的,記錄在案的方法,但是提示文本的大小必須在添加EditText之前設置,無論它是在膨脹期間還是通過直接實例化發生。

如果您希望提示在展開和折疊狀態下具有相同的大小。 您可以編寫自己的自定義TextInputLayout

package android.support.design.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.EditText

class ConstantHintSizeTextInputLayout : TextInputLayout {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    @SuppressLint("RestrictedApi")
    override fun addView(child: View?, index: Int, params: ViewGroup.LayoutParams?) {
        super.addView(child, index, params)
        if (child is EditText) {
            collapsingTextHelper.expandedTextSize = collapsingTextHelper.collapsedTextSize
        }
    }
}

重要提示:包名稱應與android.support.design.widget完全相同,因為此解決方案使用訪問包私有字段,但不涉及反射,因此速度很快

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM