Implement autoSizeTextType in android Edittext

好久不见. 提交于 2020-07-15 05:18:06

问题


Android Oreo buildToolsVersion provides a simplified way to autoresize textsize in AppCompatTextView as follows

<android.support.v7.widget.AppCompatTextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:maxWidth="300dp"
    android:background="@android:color/holo_green_light"
    app:autoSizeTextType="uniform"
    app:autoSizeMinTextSize="5sp"
    app:autoSizeMaxTextSize="50sp"
    app:autoSizeStepGranularity="4sp"
    />

Can a similar implementation be applied to AppCompatEditText since it is basically an extension of TextView? Simply applying autoSizeTextType to AppCompatEditText doesn't seem to work. Is there a way to make this work out?


回答1:


No you cannot. Please see here; it is disabled for all AppCompatEditText since it is not supported.




回答2:


My suggestion would be use: --> https://github.com/AndroidDeveloperLB/AutoFitTextView correction https://github.com/ViksaaSkool/AutoFitEditText

See this thread for more info: --> Auto-fit TextView for Android

General Article on mention library by author: --> https://viksaaskool.wordpress.com/2014/11/16/using-auto-resize-to-fit-edittext-in-android/




回答3:


I had an specific case which was a single line EditText, so I'm posting my solution just in case to be helpful to someone...

private val initialTextSize: Float by lazy {
    resources.getDimensionPixelSize(R.dimen.default_text_size).toFloat()
}

private fun updateTextSize(s: CharSequence) {
    // Auto resizing text
    val editWidth = myEditText.width
    if (editWidth > 0) { // when the screen is opened, the width is zero
        var textWidth = myEditText.paint.measureText(s, 0, s.length)
        if (textWidth > editWidth) {
            var fontSize = initialTextSize
            while (textWidth > editWidth && fontSize > 12) { // minFontSize=12
                fontSize -= 1
                myEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
                textWidth = myEditText.paint.measureText(s, 0, s.length)
            }
        // As long the text grows, the font size decreases, 
        // so here I'm increasing it to set the correct text s
        } else {
            var fontSize = myEditText.textSize
            while (textWidth <= editWidth && fontSize < initialTextSize) {
                fontSize = min(fontSize + 1, initialTextSize)
                myEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
                textWidth = myEditText.paint.measureText(s, 0, s.length)
            }
        }
    }
}

Then you must call this function from a TextWatcher attached to the EditText.

private fun createTextWatcher() = object : SimpleTextWatcher() {
    override fun beforeTextChanged(s: CharSequence?, 
        start: Int, count: Int, after: Int
    ) {
        super.beforeTextChanged(s, start, count, after)
        s?.let { updateTextSize(it) }
    }

    override fun onTextChanged(
        s: CharSequence?,
        start: Int,
        before: Int,
        count: Int
    ) {
        // Do nothing
    }

    override fun afterTextChanged(s: Editable?) {
        super.afterTextChanged(s)
        s?.let { updateTextSize(it) }
    }
}


来源:https://stackoverflow.com/questions/46406737/implement-autosizetexttype-in-android-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!