Android 最近添加了对根据视图大小以及最小和最大文本大小调整 TextViews 文本大小的支持.
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html
Android recently added the support for resizing TextViews text size based on the view size and the min and max text size.
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html
很遗憾,它们不支持 EditTexts,那么 EditText 有没有其他替代品?
Unfortunately, they don't support EditTexts, so Is there any other alternatives for EditText?
我和你一样被卡住了,EditText 是 TextView 的子对象但不支持自动调整大小 ¿?¿?
I was stuck as you, how EditText is child of TextView but don't support autosize ¿?¿?
我通过某种 hack 实现了这一点.首先我看到了 TextView 代码在 EditTextView 上复制和实现为扩展(在 Kotlin 中),但是.. 有很多方法,所以最后我放弃了这个选项.
I have achieve this with some kind of hack. First I saw the TextView code to copy and implement as extension (in Kotlin) on EditTextView, but.. there is a bulk of methods, so endly I discard that option.
我所做的是使用不可见的 TextView(是的,我知道这是一个完整的 hack,对此我不太满意,但 Google 应该为此感到羞耻)
What I have do, it's to use a TextView invisible (yes I know is a complete hack, am not very happy with that but Google should be ashamed about this)
这是我的 xmls
<TextView android:id="@+id/invisibleTextView"
android:layout_height="0dp"
android:layout_width="match_parent"
android:focusable="false"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="@dimen/text_min"
app:autoSizeMaxTextSize="@dimen/text_max"
app:autoSizeStepGranularity="@dimen/text_step"
android:textAlignment="center"
app:layout_constraintLeft_toLeftOf="@id/main"
app:layout_constraintRight_toRightOf="@id/main"
app:layout_constraintTop_toBottomOf="@id/textCount"
app:layout_constraintBottom_toBottomOf="@id/main"
android:visibility="invisible"
tool:text="This is a Resizable Textview" />
<EditText android:id="@+id/resizableEditText"
android:layout_height="0dp"
android:layout_width="match_parent"
android:textAlignment="center"
app:layout_constraintLeft_toLeftOf="@id/main"
app:layout_constraintRight_toRightOf="@id/main"
app:layout_constraintTop_toBottomOf="@id/textCount"
app:layout_constraintBottom_toBottomOf="@id/main"
android:maxLength="@integer/max_text_length"
tool:text="This is a Resizable EditTextView" />
注意:重要的是两个视图具有相同的宽度/高度
Note: It's important both view have the same width/height
然后在我的代码中,我使用来自这个 textview 的自动计算来在我的 EditTextView 上使用.
Then on my code I use the autocalculations from this textview to use on my EditTextView.
private fun setupAutoresize() {
invisibleTextView.setText("a", TextView.BufferType.EDITABLE) //calculate the right size for one character
resizableEditText.textSize = autosizeText(invisibleTextView.textSize)
resizableEditText.setHint(R.string.text_hint)
resizableEditText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(editable: Editable?) {
resizableEditText.textSize = autosizeText(invisibleTextView.textSize)
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
textCount.text = currentCharacters.toString()
val text = if (s?.isEmpty() ?: true) getString(R.string.text_hint) else s.toString()
invisibleTextView.setText(text, TextView.BufferType.EDITABLE)
}
})
}
private fun autosizeText(size: Float): Float {
return size / (resources.displayMetrics.density + MARGIN_FACTOR /*0.2f*/)
}
请注意,要更改提示的大小,我使用此 Android EditText 提示大小.
As note, for change the size of hint i use this Android EditText Hint Size.
我知道这是一个艰难的解决方法,但至少我们确信即使在未来版本上进行可调整大小的更改时,它仍能继续工作,而专有或废弃的 github 库将失败.
I know it's a hard workaround, but at least we are sure this will continue working even when resizable change on future versions, while a propetary or abandoned github lib will fail.
我希望有一天,谷歌会听到我们的声音并在孩子身上实现这个奇妙的功能,我们可以避免所有这些事情
I hope some day, google hear us and implement on childs this wonderfull feature, and we could avoid all this stuff
希望对你有帮助
这篇关于自动调整 EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!