[英]textView change to shorter text dynamically instead of ellipsize
I have this code: 我有这个代码:
final ViewTreeObserver[] viewTreeObserver = {myAcco
viewTreeObserver[0].addOnPreDrawListener(
new OnPreDrawListener() {
@Override
public boolean onPreDraw() {
int chipWidth =
myAccountView.getMeasuredWidth()
- myAccountView.getPaddingLeft()
- myAccountView.getPaddingRight();
if (chipWidth > 0) {
myAccountView.setText(
setChipTextWithCorrectLength(
getContext().getString(R.string.og_my_account_desc_long_length),
getContext().getString(R.string.og_my_account_desc_meduim_length),
getContext().getString(R.string.og_my_account_desc_short_length),
chipWidth));
viewTreeObserver[0] = myAccountView.getViewTreeObserver();
if (viewTreeObserver[0].isAlive()) {
viewTreeObserver[0].removeOnPreDrawListener(this);
}
}
return true;
}
});
}
@VisibleForTesting
String setChipTextWithCorrectLength(
String longDesc, String mediumDesc, String shortDesc, int clipWidth) {
if (!isTextEllipsized(longDesc, clipWidth)) {
return longDesc;
}
if (!isTextEllipsized(mediumDesc, clipWidth)) {
return mediumDesc;
}
return shortDesc;
}
private boolean isTextEllipsized(String text, int clipWidth) {
TextPaint textPaint = myAccountView.getPaint();
Toast.makeText(this.getContext(), "textPaint.measureText(text) = "+textPaint.measureText(text)+" clipWidth ="+ clipWidth,
Toast.LENGTH_LONG).show();
return textPaint.measureText(text) > clipWidth;
}
The code should change the text in a textView dynamically when ellipsize is needed. 当需要ellipsize时,代码应动态更改textView中的文本。
However, when i run the application I see sometimes the view (clip) width ==
the long text width and there is still ellipsize in the UI. 但是,当我运行应用程序时,我看到有时视图(剪辑)宽度==
长文本宽度,并且UI中仍然存在椭圆形。
I see that happens when the textView is not visible
and toggeled to be visible
. 我看到当textView not visible
并且被标记为visible
时会发生这种情况。
Should I calculate the vlip (view) width differently? 我应该以不同的方式计算vlip(视图)宽度吗?
Any special way to measure textView before becomes visible
? 之前测量textView的任何特殊方法都visible
?
You should check with 你应该检查一下
getEllipsisCount()
to see the source of the problem. 看问题的根源。
Definition : Returns the number of characters to be ellipsized away, or 0 if no ellipsis is to take place. 定义 :返回要进行椭圆化的字符数,如果不进行省略则返回0。
Yes you can use textView.getLayout().getEllipsisStart(0)
,make sure your textView is singleline , you can do this by adding android:maxLines="1"
to your texView. 是的,您可以使用textView.getLayout().getEllipsisStart(0)
,确保您的textView.getLayout().getEllipsisStart(0)
是单行,您可以通过将android:maxLines="1"
到您的texView来实现。 It return the index from which the text gets truncated else returns 0. 它返回文本被截断的索引,否则返回0。
Depending on your observation , it might be that the view is not recalculating after changing the visibility to visible again, try to use textView.setVisibility(View.GONE)
instead of textView.setVisibility(View.INVISIBLE);
根据您的观察,可能是在将可见性再次更改为可见后不重新计算视图,尝试使用textView.setVisibility(View.GONE)
而不是textView.setVisibility(View.INVISIBLE);
.This might cause the preDraw()
to be called again. 。这可能会导致再次调用preDraw()
。
You might also consider using addOnGlobalLayoutListener()
to keep the track of visibilty changes of your view, for reference . 您还可以考虑使用addOnGlobalLayoutListener()
来跟踪视图的可见变化,以供参考 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.