此代碼用於顯示contentview的寬度和高度,代碼在onCreate()方法中調用setContentView()方法之後運行,但它的結果寬度爲零和零高度。但是,contentview在屏幕上打開,並且對於寬度或高度都不爲0。這裏出現了什麼問題,以及如何顯示contentview的寬度和高度?contentView返回0的寬度和高度
contentview對象不是null,我使用了toString()語句來檢查。
如果這不好,那麼有沒有更好的方式來獲得像素的contentView的高度和寬度?
ViewGroup contentViewObj;
View v;
...
@Override
public void onContentChanged() {
// TODO Auto-generated method stub
super.onContentChanged();
//v = (ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content);
v = (View) getWindow().getDecorView().findViewById(android.R.id.content);
Toast.makeText(MainActivity.this, "width of contentview " + v.getWidth()
+ " " + "height of contentView " + v.getHeight(), Toast.LENGTH_SHORT).show();
}