如何检查是否已修剪TextView字符串(选取框)?

Web*_*x23 3 android textview

如果a TextView的父元素中没有足够的空间,我将显示一个图标.该文本或图标上的选项卡将用于调用带有完整字符串的警报对话框.所以我需要知道一个人TextView是否已经被摧毁了.

Sun*_*hoo 5

拼接TextView的宽度,并计算将在textview中显示的文本宽度.如果文本的宽度大于textView的宽度,则意味着您必须调用对话框,因为文本将被删除.否则,文本完全适合TextView,没有任何问题,因此不需要对话框.

使用以下代码.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
//  System.out.println("...111Height..."+mainLayout.getMeasuredHeight());

    isMarqueed("I am fine here. How r u", textView.getWidth(), textView);
    isMarqueed("I am fine", textView.getWidth(), textView);
}


private boolean isMarqueed(String text, int textWidth, TextView tv) { 
    Paint testPaint = new Paint();
    testPaint.set(tv.getPaint());
    boolean isMarquee = true;
    if (textWidth > 0) {
        int availableWidth = (int) (textWidth - tv.getPaddingLeft() - tv.getPaddingRight()-testPaint.measureText(text));
        System.out.println("...available width..."+availableWidth);
//        tv.setText(text);
        isMarquee = false;
    }
    return isMarquee;
}
Run Code Online (Sandbox Code Playgroud)

谢谢迪帕克

  • 这是如何被接受的答案?如果您传递适当的宽度,则条件“textWidth > 0”始终为真。您是否应该检查 `availableWidth` 变量以便将 `isMarquee` 变量设置为 true 或 false?@SunilKumarSahoo (3认同)