gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:04:58 25 4
gpt4 key购买 nike

如果 TextView 在其父元素中没有足够的空间,我将显示一个图标。该文本或图标上的选项卡将用于调用带有完整字符串的警告对话框。所以我需要知道 TextView 是否已被修剪。

最佳答案

计算 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;
}

谢谢迪帕克

关于android - 如何检查 TextView 字符串是否已被修剪(选取框)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6187203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com