2009-12-08 97 views

回答

1

我這樣做,用我的根佈局,問題是,當他們被第一次跑,你不能獲得你的onCreate /在onStart /方法的onResume此信息(寬度和高度)。

之後的任何時刻,大小可從根佈局(LinearLayout中/ FrameLayout裏/ TableLayout /等),在我的情況我是用檢索的FrameLayout:

FrameLayout f = (FrameLayout) findViewById(R.id.layout_root); 
Log.d(TAG, "width " + f.getMeasuredWidth()); 
Log.d(TAG, "height " + f.getMeasuredHeight()); 

或:

FrameLayout f = (FrameLayout) findViewById(R.id.layout_root); 
Log.d(TAG, "width " + f.getRight()); 
Log.d(TAG, "height " + f.getBottom()); 

既然已經有活動的高度,要知道系統欄的高度只是從活動的高度減去全屏高度。

如何讓屏幕的高度在這裏解釋說:從這裏編輯 Get screen dimensions in pixels

-------------------------- ------------------------------

我找到一種方式來獲得對onCreted方法這個信息,但只有當你有更多的活動。就我的情況而言,我有一項主要活動稱爲第二項活動。

在main_activity之前我所說的second_activity,由新Itent,我可以添加爲second_activity額外的信息。以下是你需要的第一個活動代碼:

Intent i = new Intent(this, SecondActivity.class); 
i.putExtra("width", f.getMeasuredWidth()); 
i.putExtra("height", f.getMeasuredHeight()); 
startActivity(i); 

之後,在第二次的活動,你可以檢索你的第二個活動的onCreate方法這個信息這樣做:

int width = getIntent().getExtras().getInt("width"); 
int height = getIntent().getExtras().getInt("height");