android onCreate() 获取view控件宽高为0
原因很简单的,view高度是在周期
onMeasure()中测量,而onMeasure()调用又在周期onCreate()之后.所以获取到的宽高为0。具体来说是在onResume()之后调用。
解决方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_size);
size = (TextView) findViewById(R.id.size);
size.post(new Runnable() {
@Override
public void run() {
size.setText("高度:"+size.getHeight()+"宽度:"+size.getWidth());
}
});
}