Android标题栏大小

时间:2009-12-08 18:30:44

标签: android titlebar

如何获取活动标题栏的大小和系统栏的大小?

1 个答案:

答案 0 :(得分:1)

我使用我的根布局做了那个,问题是你的onCreate / onStart / onResume方法在第一次运行时无法获得这个信息(宽度和高度)。

之后的任何时刻,都可以从你的根布局(LinearLayout / FrameLayout / TableLayout / etc)中检索大小,在我的情况下,我使用的是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方法上获取此信息的方法,但前提是您有多个活动。在我的情况下,我有一个主要活动,调用第二个活动。

在调用second_activity之前的main_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");