Android TextView lineCount 在设置椭圆大小时 return 没有正确的值
Android TextView lineCount doesn't return correct value when ellipsize is set
我想展开和折叠我的 TextView。所以我为此制作了这些扩展功能。
private const val MAX_LINES_PROPERTY_NAME = "maxLines"
private const val EXPAND_ANIMATION_DURATION = 200L
fun TextView.expand() {
val animation = ObjectAnimator.ofInt(this, MAX_LINES_PROPERTY_NAME, this.lineCount)
animation.setDuration(EXPAND_ANIMATION_DURATION).start()
}
fun TextView.collapse(numLines: Int) {
val animation = ObjectAnimator.ofInt(this, MAX_LINES_PROPERTY_NAME, numLines)
animation.setDuration(EXPAND_ANIMATION_DURATION).start()
}
像这样设置TextView时出现问题
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:ellipsize="end"
android:text="Long text"
android:textColor="@color/black"
android:textSize="16sp">
显然 textView.lineCount returns 3 当 ellipsize 设置为“end”时。当我删除那行代码时,一切正常。有谁知道我该如何避免这种情况?
我发现了一些设置椭圆大小和获取整个 TextView 的行数的 hacky 方法。
首先,我删除了 xml 中的 android:ellipsize="end"
行。
之后,我在本地保存了 lineCount,然后以编程方式设置了 ellipsize。
private var textLineCount = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
textView.post {
textLineCount = textView.lineCount
textView.ellipsize = TextUtils.TruncateAt.END
}
}
现在我在本地有 textLineCount,我只是将它传递到我的扩展 TextView 函数中,我是这样修改的
fun TextView.expand(maxLines: Int? = null) {
val animation = ObjectAnimator.ofInt(this, MAX_LINES_PROPERTY_NAME, maxLines ?: this.lineCount)
animation.setDuration(EXPAND_ANIMATION_DURATION).start()
}
就是这样。我知道这不是最快乐的解决方案,但它确实有效。如果有人知道更好,请分享您的解决方案。
我想展开和折叠我的 TextView。所以我为此制作了这些扩展功能。
private const val MAX_LINES_PROPERTY_NAME = "maxLines"
private const val EXPAND_ANIMATION_DURATION = 200L
fun TextView.expand() {
val animation = ObjectAnimator.ofInt(this, MAX_LINES_PROPERTY_NAME, this.lineCount)
animation.setDuration(EXPAND_ANIMATION_DURATION).start()
}
fun TextView.collapse(numLines: Int) {
val animation = ObjectAnimator.ofInt(this, MAX_LINES_PROPERTY_NAME, numLines)
animation.setDuration(EXPAND_ANIMATION_DURATION).start()
}
像这样设置TextView时出现问题
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:ellipsize="end"
android:text="Long text"
android:textColor="@color/black"
android:textSize="16sp">
显然 textView.lineCount returns 3 当 ellipsize 设置为“end”时。当我删除那行代码时,一切正常。有谁知道我该如何避免这种情况?
我发现了一些设置椭圆大小和获取整个 TextView 的行数的 hacky 方法。
首先,我删除了 xml 中的 android:ellipsize="end"
行。
之后,我在本地保存了 lineCount,然后以编程方式设置了 ellipsize。
private var textLineCount = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
textView.post {
textLineCount = textView.lineCount
textView.ellipsize = TextUtils.TruncateAt.END
}
}
现在我在本地有 textLineCount,我只是将它传递到我的扩展 TextView 函数中,我是这样修改的
fun TextView.expand(maxLines: Int? = null) {
val animation = ObjectAnimator.ofInt(this, MAX_LINES_PROPERTY_NAME, maxLines ?: this.lineCount)
animation.setDuration(EXPAND_ANIMATION_DURATION).start()
}
就是这样。我知道这不是最快乐的解决方案,但它确实有效。如果有人知道更好,请分享您的解决方案。