Android多行文本视图,检查文本是否合适,或者检查文本视图是否已满

Android多行文本视图,检查文本是否合适,或者检查文本视图是否已满,android,android-layout,textview,Android,Android Layout,Textview,我有一个Android应用程序布局,其中包含一个多行文本视图。当屏幕处于纵向时,TextView可以显示与在横向模式下运行时不同的文本长度。此外,在小屏幕上运行时,TextView可以显示与在大屏幕上运行时不同的文本长度 有没有办法检查文本是否合适或是否会被截断?或者有没有办法检查文本视图是否已满 问题是TextView可能包含不同数量的行,这取决于它是横向、纵向、小屏幕还是大屏幕等 谢谢你的建议 致以最良好的祝愿 James要检查多行(或不)文本视图是否将被截断,请检查 或者,您是否考虑过使用

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

有没有办法检查文本是否合适或是否会被截断?或者有没有办法检查文本视图是否已满

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

谢谢你的建议

致以最良好的祝愿


James

要检查多行(或不)文本视图是否将被截断,请检查

或者,您是否考虑过使用滚动文本视图?(字幕)。。如果文本对于给定的宽度太长,文本将在何处滚动(水平、动画)

下面是布局文件中的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)"/>

基本上,当方向模式改变时,您需要计算文本视图的大小和文本的大小。请尝试
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.
        }
    });
以下是计算文本大小的代码:

private int getTextSize(String your_text){
    Paint p = new Paint();
    //Calculate the text size in pixel
    return p.measureText(your_text);
}
希望对您有所帮助。

对于在多行文本视图中测量文本高度的问题,我找到了一个“厚颜无耻”的解决方案:-

//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
}
但是,此解决方案有一些警告:-

//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(),文本中的标记可能导致单个行比此高度高或短,布局可能包含额外的第一行或最后一行填充。看

其次,应用程序需要计算TextView的实际高度(以像素为单位),并且(在我的布局中)它可能没有TextView.getHeight()那么简单,并且计算可能因布局而异

我建议避免线性布局,因为文本视图的实际像素高度可能因文本内容而异。我正在使用RelativeLayout(请参阅)

使用此RelativeLayout,我可以计算我的TextView高度,如下所示:-

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

问候,


詹姆斯这些答案对我来说不太合适。这就是我最后做的

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());
Paint-measurePaint=new-Paint(myTextView.getPaint());
float pWidth=measurePaint.measureText(“这是我的一大行长文本,适合这里吗?”);
float labelWidth=myTextView.getWidth();
int maxLines=myTextView.getMaxLines();
而(labelWidth>0&&pWidth/maxLines>labelWidth-20){
float textSize=measurePaint.getTextSize();
measurePaint.setTextSize(textSize-1);
pWidth=measurePaint.measureText(“这是我的一大行文本,适合这里吗?”);
if(textSize

我并不是说这适用于所有情况,因为我在这里肯定是在抄近路,但总体思路是用textview的画笔测量文本,并不断缩小它,直到它适合textview

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

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
用法


这对我来说不起作用,因为“p.measureText(your_text)”只计算文本宽度。它不计算多行文本视图的文本高度。我现在找到了另一个解决方案,我将很快添加它。关于您的XML,这对我不起作用,因为它设置了singleLine=“true”,这不是多行文本视图。关于检查这篇文章,这对我不起作用,因为“testPaint.measureText(text)”只计算文本宽度。它不计算多行文本视图的文本高度。我现在找到了另一个解决方案,我将很快添加它。这个答案不涉及多行文本视图。它只检查文本是否会填充文本的宽度。问题不仅仅是询问多行;它还询问在文本视图中的“装配”问题。。。