2009-12-08 70 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");