【问题标题】:How to check if a TextView's text exceeds the max lines如何检查 TextView 的文本是否超过最大行数
【发布时间】:2018-10-08 20:44:25
【问题描述】:

有没有办法检查 TextView 的文本是否超过最大行数?我找到了TextView.getLineCount(),但这只能告诉我我是否使用了最大行数,而不是我是否超过了它。

编辑:
到目前为止,我有:

text_view.post(new Runnable() {
            @Override
            public void run() {
                if (text_view.getLineCount() < text_view.getMaxLines()) {
                    // do stuff
                }
            }
});

但这不会检测它是否超过最大行数,即如果它使用大于或等于最大行数,if 语句将通过。这是因为如果它使用超过最大行数,那么 getLineCount 只会返回最大行数,永远不会更多。

请注意,文本视图的文本在我的代码中以编程方式设置得更高,而不是在 XML 中硬编码。

【问题讨论】:

  • 超出最大行数是什么意思?你想给出最大行数吗?
  • @Davide 如果它超过了 XML 中设置的 TextView 的最大行数。要在 Java 中访问,它是 TextView.getMaxLines()。因此,例如,如果文本需要 10 行但最大行数为 5,则第 6-10 行将被截断,我想检测何时发生这种情况。
  • 不确定这是否是您要查找的内容,但您可以使用this answer 中提出的算法来计算Textview 有多少完全可见的行。
  • @Onik 你能把它作为答案吗?我找到了一种方法(并在此处将其写为答案),但这有点像解决方法...
  • @android 开发者,实际上我不知道关于 OP 要求的解决方案可以使用什么标准。您是否尝试过解决方案?在我看来,Paradox 可能会设置 android:minLines=5 并检查页数是否超过 2。但是,我认为有更好的解决方案,并且在测试之前不想发布我的假设。

标签: android textview


【解决方案1】:

不确定是否有更好的方法,但我通过检查如果我将 maxLines 设置得更高会发生什么得到它,并查看在测量后它是否仍然适合原始 maxLines。

如果您还想很好地从 TextView 的折叠模式到展开模式设置动画,请查看我的解决方案 here

如果没有,这是我对这个非常具体的问题的解决方案:

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val expectedWidthOfTextView = resources.displayMetrics.widthPixels
        val originalMaxLines = textView.maxLines
        if (originalMaxLines < 0 || originalMaxLines == Integer.MAX_VALUE)
            Log.d("AppLog", "already unbounded textView maxLines")
        else {
            textView.maxLines = originalMaxLines + 1
            textView.measure(
                View.MeasureSpec.makeMeasureSpec(expectedWidthOfTextView, View.MeasureSpec.AT_MOST),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
            )
            Log.d("AppLog", "lines:${textView.lineCount}/$originalMaxLines")
            if (textView.lineCount <= originalMaxLines)
                Log.d("AppLog", "fit in original maxLines")
            else
                Log.d("AppLog", "exceeded original maxLines")
            textView.maxLines = originalMaxLines
        }

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent" android:orientation="vertical" android:gravity="center"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:id="@+id/textView" android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp" android:background="#1100ff00"
            android:ellipsize="end" android:maxLines="4"
            android:paddingEnd="16dp" android:paddingStart="16dp"
            android:textColor="#c1000000" android:textSize="14sp"
            android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."/>

</LinearLayout>

【讨论】:

    【解决方案2】:

    你可以这样做:

    textView.doOnLayout {
        if (textView.isEllipsized()) {
            // Do Something
        }
    }
    
    fun TextView.isEllipsized(): Boolean {
        val textPixelLength = paint.measureText(text.toString())
        val numberOfLines = ceil((textPixelLength / measuredWidth).toDouble())
        return lineHeight * numberOfLines > measuredHeight
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-14
      • 2014-04-01
      相关资源
      最近更新 更多