獲取控件寬高

在onCreate()裏是獲取不到控件的width,height的,這個時候控件都沒有measure,layout完畢,所以獲取到的結果都是0。要獲取控件的寬度、高度必須在measure、layout過程完畢之後。


//在窗口第一次獲得焦點的時候,獲取控件的width,height。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
TextView tv = new TextView(this);
Log.v("tvJT.getWidth()", ""+tv.getWidth());
Log.v("tvJT.getHeight()", ""+tv.getHeight());
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章