我有芯片视图。
我想检查文本是否为椭圆形,如果是的话-用较短的文本替换文本(运行时)。
我看过这个code来检查芯片中的文本是否为椭圆形。
Layout l = textview.getLayout();
if (l != null) {
int lines = l.getLineCount();
if (lines > 0)
if (l.getEllipsisCount(lines-1) > 0)
Log.d(TAG, "Text is ellipsized");
}
但是我不知道我应该在什么生命周期事件中调用此方法,对于这一行
Layout l = myAccountView.getLayout();
我得到l = null
我有查看生命周期(保存我的芯片的框架布局)
我尝试检查onDraw()
和onLayout()
我也尝试过从对话框中调用该框架
但是我知道通货膨胀是最重要的,因此它也会在l= null
上返回setContentView()
。
答案 0 :(得分:0)
方法1:请在myAccountView.onPreDraw();
之前致电myAccountView.getLayout()
方法2:使用ViewTreeObserver
ViewTreeObserver vtObserver= myAccountView.getViewTreeObserver();
vtObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Layout layout = myAccountView.getLayout();
}
});