android 获取textView的行数为0,获取控件的宽高为0的解决办法

来源:互联网 发布:平板炒股软件 编辑:程序博客网 时间:2024/06/05 14:12

有时候我们在获取textView的行数时、或者是获取控件的宽和高时,(妈蛋,刚写完的那版app里边就有好几处用到,恶心的不行不行的)会返回0的状况,那么我们应该怎么解决呢

一、获取textView的行数为0的状况

 holder.title.post(new Runnable() {            @Override            public void run() {                // TODO Auto-generated method stub                if(holder.title.getLineCount()==2)                {                    holder.description.setVisibility(View.GONE);                }            }        });

因为我是在getview里边需要这个功能,正好查到的也是,所以就偷懒直接搬过来的

二、获取控件的宽和高为0(一般貌似是在onCreate()中)

 ViewTreeObserver vtophoto=mUserPhoto.getViewTreeObserver();        vtophoto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            @Override            public void onGlobalLayout() {                mUserPhoto.getViewTreeObserver().removeGlobalOnLayoutListener(this);                int width = mUserPhoto.getWidth();                int height=mUserPhoto.getHeight();            }        });

ok,就这样了,我快饿坏了

0 0
原创粉丝点击