Android标题栏大小

标签 android titlebar

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

最佳答案

我使用我的根布局做到了这一点,问题是当它们第一次运行时,您无法在 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() );

既然你已经有了activity的高度,要知道系统栏的高度只是用activity的高度减去全屏高度。

这里解释了如何获取屏幕的高度: Get screen dimensions in pixels

从这里编辑 ------------------------------------------ --------------

我找到了一种在 onCreted 方法上获取此信息的方法,但前提是您有多个 Activity 。就我而言,我有一个调用第二个 Activity 的主要 Activity 。

在我调用 second_activity 之前的 main_activity 上,通过一个新的 Itent,我可以为 second_activity 添加额外的信息。这是第一个 Activity 所需的代码:

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

在您的第二个 Activity 之后,您可以在第二个 Activity 的 onCreate 方法上检索此信息:

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

关于Android标题栏大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1868936/

相关文章:

android - 亚洲开发银行 + 三星 Galaxy

android - 标题栏中的按钮如何到达它

c# - 不一致的 TitleBar 高度返回

android - Phonegap Android 显示标题栏

android - 无法导入 android.support.v4.apps.NavUtils

android - Mono Android 双指缩放同时覆盖 ondraw

android - 如何在 RxJava 2 中的 groupBy 之后获取列表?

Android工具栏菜单文字颜色

java - 如何设置标题栏的背景?

java - 无法隐藏标题栏