【问题标题】:Android multiline TextView , check if text fits , or check is TextView is fullAndroid 多行 TextView ,检查文本是否适合,或者检查 TextView 是否已满
【发布时间】:2013-08-07 21:42:39
【问题描述】:

我有一个包含多行 TextView 的 Android 应用程序布局。当屏幕处于纵向时,TextView 可以显示与在横向模式下运行时不同的文本长度。此外,在小屏幕上运行时,TextView 可以显示与在大屏幕上运行时不同的文本长度。

有什么方法可以检查文本是否适合或将被截断?或者有什么方法可以检查 TextView 是否已满?

问题是 TextView 可能包含不同数量的行,具体取决于它是横向、纵向、小屏幕、大屏幕等。

感谢您的建议,

最好的问候,

詹姆斯

【问题讨论】:

    标签: android android-layout textview


    【解决方案1】:

    这些答案对我来说不是很好。这就是我最终做的事情

    Paint measurePaint = new Paint(myTextView.getPaint());
    float pWidth = measurePaint.measureText("This is my big long line of text. Will it fit in here?");
    float labelWidth = myTextView.getWidth();
    int maxLines = myTextView.getMaxLines();
    
    while (labelWidth > 0 && pWidth/maxLines > labelWidth-20) {
        float textSize = measurePaint.getTextSize();
        measurePaint.setTextSize(textSize-1);
        pWidth = measurePaint.measureText("This is my big long line of text. Will it fit in here?");
        if (textSize < TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 7,
                getContext().getResources().getDisplayMetrics())) break;
    }
    
    myTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, measurePaint.getTextSize());
    

    我并不是说这适用于所有情况,因为我肯定会在这里偷工减料,但总体思路是使用 textview 的油漆测量文本并不断缩小它,直到它适合 textview。

    【讨论】:

      【解决方案2】:

      我找到了一个“厚颜无耻”的解决方案来解决在 MULTILINE TextView 中测量文本高度的问题-

      //Calculate the height of the text in the MULTILINE TextView
      int textHeight = textView.getLineCount() * textView.getLineHeight();
      if (textHeight > textViewHeight) {
          //Text is truncated because text height is taller than TextView height
      } else {
          //Text not truncated because text height not taller than TextView height
      }
      

      但是这个解决方案有一些警告:-

      首先关于 getLineHeight() ,文本中的标记可能会导致个别行高于或低于此高度,并且布局可能包含额外的第一行或最后一行填充。见http://developer.android.com/reference/android/widget/TextView.html#getLineHeight()

      其次,应用程序需要以像素为单位计算 TextView 的实际高度,并且(就我的布局而言)它可能不像 textView.getHeight() 那样简单,并且计算可能因布局而异布局。

      我建议避免 LinearLayout,因为 TextView 的实际像素高度可能会因文本内容而异。我正在使用 RelativeLayout(请参阅 http://pastebin.com/KPzw5LYd)。

      使用 this RelativeLayout,我可以计算 my TextView 的高度如下:-

      //Calculate the height of the TextView for layout "http://pastebin.com/KPzw5LYd"
      int textViewHeight = layout1.getHeight() - button1.getHeight() - button2.getHeight();
      

      希望对你有帮助,

      问候,

      詹姆斯

      【讨论】:

        【解决方案3】:

        基本上,当方向模式改变时,您需要计算 textview 的大小和文本的大小。尝试ViewTreeObserver.OnGlobalLayoutListener 这样做。

        内部改变方向方法:

        main_view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        
                @Override
                public void onGlobalLayout() {
                    main_view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    //Calculation goes here
                    int text_size = getTextSize(text_view.getText().toString());
                    int text_view_size = text_view.getLayoutParams().width;
                    //Compare your text_size and text_view_size and do whatever you want here.
                }
            });
        

        这里是计算text_size的代码:

        private int getTextSize(String your_text){
            Paint p = new Paint();
            //Calculate the text size in pixel
            return p.measureText(your_text);
        }
        

        希望对您有所帮助。

        【讨论】:

        • 这对我不起作用,因为“p.measureText(your_text)”只计算文本宽度。它不计算 MULTILINE TextView 的文本高度。我现在找到了另一个解决方案,我会尽快添加。
        【解决方案4】:

        要检查多行(或不)TextView 是否会被截断,请检查this post

        或者,您是否考虑过使用滚动文本视图? (选框).. 如果文本对于给定的宽度来说太长,将滚动到哪里(水平,动画)?

        这是布局文件中的一个示例 TextView,它具有以下一些特征:

        <TextView
            android:id="@+id/sometextview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:singleLine="true"
            android:scrollHorizontally="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:freezesText="true"
            android:textColor="#808080"
            android:textSize="14sp"
            android:text="This is a long scrolling line of text.. (etc)"/>
        

        【讨论】:

        • 关于您的 XML,这对我不起作用,因为它设置了 singleLine="true" 这不是 MULTILINE TextView。关于检查这篇文章,这对我不起作用,因为“testPaint.measureText(text)”只计算文本宽度。它不计算 MULTILINE TextView 的文本高度。我现在找到了另一个解决方案,我会尽快添加。
        • 该答案不涉及多行文本视图。它只检查文本是否会填充 textveiw 的 width
        • 问题不只是询问多行;它还询问文本视图中的“拟合”...
        【解决方案5】:

        只需检查 textView.getLineCount(),如果行数 > 1 那么你的文本是多行的

        【讨论】:

          【解决方案6】:

          Kotlin 中的这个扩展函数对我有用,只要确保在视图已经布置好时调用它,例如view.post() 对我来说是个好地方;

          /**
           * Make sure to call this method only when view layout is finished, e.g. view.post() is a good place to check this
           */
          fun TextView.isTruncated() = (lineCount * lineHeight) > height
          

          用法

          textView.post {
              if(isTruncated()) {
                  // Do something
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-03-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-10-08
            相关资源
            最近更新 更多