【问题标题】:How to find out whether the text to be placed in a TextView will fit in a single row or not如何确定要放置在 TextView 中的文本是否适合单行
【发布时间】:2013-01-10 08:04:02
【问题描述】:

有没有办法确定要放置在 TextView 中的文本内容是否适合单行?

我想要实现的内容显示在附加图像中(它是 listView 的一部分)。问题与textView#3 有关。如果它太大,我想把它放在textView#2下面,但如果内容足够短,我想把它放在textView#2的右边(row#3中看到的场景是我想要逃避的)

那么,有人吗?.. 我该如何解决这个特殊问题? 我想这无法通过我的列表视图行的单一布局来实现..

【问题讨论】:

    标签: android listview textview


    【解决方案1】:

    这是可能的。您应该测量文本大小并将其与 TextView 大小进行比较。如果文本宽度 > textView 宽度比它太长。

    您可以从this post了解文本测量。

    您也可以使用TextView的内置功能,使其成为单行并设置文本椭圆大小的方法。

    【讨论】:

    • 我需要显示全文。它很可能不是一个大段落。也许〜10个字。
    • 如果要在一行中显示全文,请使用 AutoResizeTextView。它将减小字体大小以将 textView 中的所有文本放在一行中。
    【解决方案2】:

    一种解决方案(不是一个漂亮的解决方案)是对您的字符串在 textView 中占用的空间进行一些数学运算。在我的特殊情况下,我添加了 2 个 textViews (3 & 3B) & 取决于我的数学计算结果 - 我会只使用一个并隐藏另一个。

    详情:

    1. 在你的适配器的getView方法中找出父listView的宽度

      public View getView(int position, View convertView, ViewGroup parent) { int listViewWidth = parent.getWidth();

    2. 计算放置在同一行的其他 textView 占用的空间(在我的例子中只是 textView2);

      油漆油漆 = textViewX.getPaint(); occupiedWidth = (int) (occupiedWidth +paint.measureText("要放置在textview中的文本"));

    3. 比较要放置在最后一个 textview 中的文本占用的空间(在我的 screenShot textView3 中,计算宽度的公式相同)并与剩余空间比较 (listViewWidth - occupiedWidth)

    4. 在正确的 textView 上设置文本并隐藏另一个
    5. ..根据需要改进代码

    【讨论】:

      【解决方案3】:

      没有内置函数可以比较这个。

      在做了很多研究和代码之后,我已经构建了自己的函数来做到这一点......

      1) 将此代码复制到您的项目中

      
      
      import android.app.Activity;
      import android.content.res.Resources;
      import android.util.DisplayMetrics;
      import android.util.TypedValue;
      import android.widget.TextView;
      
      public class TextMeasure {
      
      
          public static boolean isTextCanFitInTextView(TextView textView, String txt , float allowWidthInDp , Activity activity){
      
              String backupText = textView.getText().toString();
      
              textView.setText(txt);
              textView.measure(0, 0);       //must call measure!
              float textViewSize_px = textView.getMeasuredWidth(); //get width in px
      
              // Converts dip into its equivalent px
              float dip = allowWidthInDp;
              Resources r = activity.getResources();
              float allowView_px = TypedValue.applyDimension(
                      TypedValue.COMPLEX_UNIT_DIP,
                      dip,
                      r.getDisplayMetrics()
              );
      
              //restore textView
              textView.setText(backupText);
      
              return textViewSize_px <= allowView_px;
          }
      
      
          public static boolean isTextCanFitInTextView_matchParent(TextView textView, String txt , float totalMarginInDp, Activity activity){
      
              String backupText = textView.getText().toString();
      
              textView.setText(txt);
              textView.measure(0, 0);       //must call measure!
              float textViewSize_px = textView.getMeasuredWidth(); //get width in px
      
              // Converts dip into its equivalent px
              float dip = totalMarginInDp;
              Resources r = activity.getResources();
              float totalMarginInDp_px = TypedValue.applyDimension(
                      TypedValue.COMPLEX_UNIT_DIP,
                      dip,
                      r.getDisplayMetrics()
              );
      
              float allowView_px;
              float window_length;
      
              window_length = getDisplayWidthInPixel(activity);
      
              allowView_px = window_length - totalMarginInDp_px;
      
              //re store textView
              textView.setText(backupText);
      
              return textViewSize_px <= allowView_px;
          }
      
          private static float getDisplayWidthInPixel(Activity activity){
              DisplayMetrics metrics = new DisplayMetrics();
              activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
              return  metrics.widthPixels;
          }
      
      
      
      
      }
      

      2) 使用上面的类方法来做你的 text 和 textView 比较,例如:

      1) 知道确切的宽度,android:layout_width="300dp"

      
       TextView yourTextView = findViewById(R.id.txt);
              float widthOfTextView_inDp = 300f;      // width of textView Size in dp (densityPixel) , what you set in xml view
      
              String yourTxt = "your text need to display in single line";
      
              if (TextMeasure.isTextCanFitInTextView(yourTextView,yourTxt,widthOfTextView_inDp,this)){
                  // text can fit in to text View
                  yourTextView.setText(yourTxt);
      
              }else {
                  // text can't fit in to text View
      
                  // add your logic
              }
      
      

      2) match_parent 用来设置宽度。

      android:layout_marginStart="10dp" android:layout_marginEnd="10dp" android:layout_width="match_parent"

      
       TextView yourTextView = findViewById(R.id.txt);
              float totalMargin_inDp = 20f;      // width of text view is match_parent and (marginStart = 10dp), (marginEnd = 10dp) = 20dp, note: it is total margin from both end of mobile screen combine.
      
              String yourTxt = "your text need to display in single line";
      
              if (TextMeasure.isTextCanFitInTextView_matchParent(yourTextView,yourTxt,totalMargin_inDp,this)){
                  // text can fit in to text View
                  yourTextView.setText(yourTxt);
      
              }else {
                  // text can't fit in to text View
      
                  // add your logic
              }
      
      
      

      【讨论】:

        【解决方案4】:

        试试textview的这个属性

        android:maxLength="number of characters you want to display in textview"
        

        现在在 Activity 中检查字符串长度是否 > maxLength,而不是更改视图的位置。

        【讨论】:

        • 谢谢..但我不知道字符数.. textView 使用换行内容.. & 显然父 viewGroup/layout 宽度将决定这个 textview 可以容纳多少内容(在平板电脑上我肯定有空间..)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-07
        • 2015-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多