如何判断在一个单行TextView中放入的文本是否适合?

4
有没有一种方法可以确定要放置在TextView中的文本内容是否适合单行显示?
我想要实现的效果如附图所示(它是ListView的一部分)。这个问题涉及到textView#3。如果它太大了,我想把它放在textView#2下面,但如果内容足够短,我想把它放在textView#2的右边(row#3中看到的情况是我想避免的)。
那么,有人知道吗?如何解决这个特定的问题? 我想这不能通过单个布局来实现我的ListView行。
4个回答

2
可以实现。您需要测量文本大小并将其与TextView大小进行比较。如果文本宽度> textView宽度,则太长了。
您可以从此帖子了解有关文本测量的信息。
您还可以使用TextView的内置功能,使其成为单行,并设置文本省略方法。

我需要显示完整的文本.. 它很可能不会是一个大段落.. 大约有10个词.. - pulancheck1988
如果您想在一行中显示完整的文本,请使用AutoResizeTextView。它会减小字体大小以适应textView中的所有文本。 - Leonidos

0

没有内置函数可以进行比较。

经过多次研究和编码,我已经构建了自己的函数来完成这个任务...

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) 使用上述类方法进行文本和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
        }



0
一种解决方案(并不是很好的解决方案)是对 textView 中字符串占用的空间进行一些数学计算。在我的特定情况下,我添加了两个 textView(3 和 3B),根据我的数学计算结果,我会使用一个并隐藏另一个。
细节:
1. 在适配器的 getView 方法中找到父 listView 的宽度。 ``` public View getView(int position, View convertView, ViewGroup parent) { int listViewWidth = parent.getWidth(); } ```
2. 计算同一行中其他 textView 占用的空间(在我的情况下只有 textView2)。 ``` Paint paint = textViewX.getPaint(); occupiedWidth = (int) (occupiedWidth +paint.measureText("text to be placed in textview")); ```
3. 比较要放置在最后一个 textView 中的文本所占用的空间(在我的屏幕截图中为 textView3,使用相同的公式计算宽度)并与剩余空间(`listViewWidth - occupiedWidth`)进行比较。
4. 将文本设置在正确的 textView 上并隐藏另一个 textView。
5. 根据需要改进代码。

new layout result


-2

尝试使用TextView的这个属性

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

现在在Activity中检查字符串长度是否大于最大长度,然后更改您的视图位置。


1
谢谢,但我不知道字符数。TextView使用wrap content,显然父ViewGroup/Layout的宽度将决定这个TextView可以容纳多少内容(在平板电脑上我肯定会有空间...)。 - pulancheck1988

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接