状态栏的高度?

use*_*701 65 android

有没有办法获得状态栏+标题栏的高度?检查开发论坛显示相同的问题,但没有解决方案(我可以找到).

我知道我们可以在初始布局通过后得到它,但我希望在我的活动的onCreate()中得到它.

谢谢

Jor*_*sys 95

Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
int contentViewTop= 
    window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight= contentViewTop - StatusBarHeight;

   Log.i("*** Jorgesys :: ", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight); 
Run Code Online (Sandbox Code Playgroud)

获取onCreate()Activity 的方法上状态栏的高度,使用此方法:

public int getStatusBarHeight() { 
      int result = 0;
      int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
      if (resourceId > 0) {
          result = getResources().getDimensionPixelSize(resourceId);
      } 
      return result;
} 
Run Code Online (Sandbox Code Playgroud)

  • 这是一个重要的假设,即状态栏位于顶部.我希望始终如此. (10认同)
  • 在哪里写这段代码..它在'OnCreate'中不起作用 (2认同)

rau*_*aug 38

虽然这是一个老问题,但我发现答案不起作用onCreate():

我从这里找到了这个代码,它在该onCreate()方法中起作用

public int getStatusBarHeight() {
   int result = 0;
   int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
   if (resourceId > 0) {
      result = getResources().getDimensionPixelSize(resourceId);
   }
   return result;
}
Run Code Online (Sandbox Code Playgroud)

我希望这对遇到这个问题的任何人都有帮助.


小智 37

对于像我这样想要在XML布局中使用它的人:

<...
  android:layout_marginTop="@*android:dimen/status_bar_height"
  ... />
Run Code Online (Sandbox Code Playgroud)

不要被Android Studio混淆(2.2预览3 - 在写作的那一刻)不支持这个概念.运行时有.我从Google的源代码中获取了它.

  • 错误:(30,43)资源不公开.(在'layout_marginTop'中,值为'@ android:dimen/status_bar_height'). (11认同)
  • 拜托,你能解释@*android表示法的含义吗?谢谢 (5认同)
  • @khan不要忘记明星(*).这是@*android:dimen/status_bar_height (4认同)
  • 在API 21模拟器上它崩溃与"引起:java.lang.UnsupportedOperationException:无法转换为维度:类型= 0x1". (3认同)
  • 在api 24之前运行良好,在api 25(android 7.1.1)上崩溃。如果在布局文件上使用它,则会出现错误消息“您必须提供layout_height(width)属性”。 (2认同)

小智 8

获得状态栏高度的支持方法是使用WindowInsets从API 21+开始的类:

customView.setOnApplyWindowInsetsListener((view, insets) -> {
    // Handle insets
    return insets.consumeSystemWindowInsets();
});
Run Code Online (Sandbox Code Playgroud)

或者WindowInsetsCompat对于支持库:

ViewCompat.setOnApplyWindowInsetsListener(customView, (view, insets) -> {
    // Handle insets
    return insets.consumeSystemWindowInsets();
});
Run Code Online (Sandbox Code Playgroud)

您还可以覆盖onApplyWindowInsets视图中的方法:

public class CustomView extends View {
    @Override
    public WindowInsets onApplyWindowInsets(final WindowInsets insets) {
        final int statusBarHeight = insets.getStableInsetTop();
        return insets.consumeStableInsets();
    }
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,我建议您查看Chris Banes的演讲 - 成为主窗口钳工(此处提供幻灯片).

  • insets.consumeStableInsets() 调用有副作用。最好只调用 view.onApplyWindowInsets,它将委托回原始窗口来处理插图。 (2认同)

dvp*_*lic 7

我建议下一个代码:

Rect rect = new Rect();
Window window = activity.getWindow();
if (window != null) {
  window.getDecorView().getWindowVisibleDisplayFrame(rect);
  android.view.View v = window.findViewById(Window.ID_ANDROID_CONTENT);

  android.view.Display display = ((android.view.WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

  //return result title bar height
  return display.getHeight() - v.getBottom() + rect.top;   
}
Run Code Online (Sandbox Code Playgroud)

两个例子:

1)Device 1品牌:samsung,设备:maguro,board:tuna,cpu_abi:armeabi-v7a,显示:ICL53F.M420KREL08,制造商:samsung,型号:Galaxy Nexus,ver.发行:4.0.2,ver.sdk:14 ;

屏幕分辨率:1280 x 720.此设备上没有硬件按钮.

结果:

rect: left=0 right=720 top=72 bottom=1208;
v: left=0 right=720 top=72 bottom=1208;
display: height=1208 width=720;
correct result=72;
Run Code Online (Sandbox Code Playgroud)

设备1在屏幕顶部有标题栏,状态栏在屏幕底部有软件按钮.

2)设备2设备:bravo,board:bravo,cpu_abi:armeabi-v7a,display:FRG83G,制造商:HTC,型号:HTC Desire,ver.发行:2.2.2,ver.sdk:8,

屏幕分辨率:800 x 400.此设备具有硬件按钮.

结果:

rect: left=0 right=480 top=38 bottom=800;
v: left=0 right=480 top=0 bottom=800;
display: height=800 width=480;
correct result: phone_bar_height=38;
Run Code Online (Sandbox Code Playgroud)

设备2在屏幕顶部有标题栏,根本没有状态栏.

上面提出了两种解决方案:

A) v.getTop() - rect.top 
Run Code Online (Sandbox Code Playgroud)

(它对设备1不正确 - 它给出0而不是72)

B) display.getHeight() - v.getHeight() 
Run Code Online (Sandbox Code Playgroud)

(它对设备2不正确 - 它给出0而不是38)

变体:

 display.getHeight() - v.getBottom() + rect.top
Run Code Online (Sandbox Code Playgroud)

在这两种情况下给出正确的结果

更新

3)一个以上实施例(第三设备):品牌:LGE,设备:V909,cpu_abi:armeabi-V7A,显示:HMJ37,型号:LG-V909,ver.release:3.1,ver.sdk:12

rect: left=0 right=1280 top=0 bottom=720
v: left=0 right=1280 top=0 bottom=720
Display: height=768 width=1280
phone_bar_height=48
Run Code Online (Sandbox Code Playgroud)

设备3具有水平方向,屏幕顶部没有标题栏,屏幕底部有状态栏.

所以在这里:

int size_of_title_bar = rect.top;
int size_of_status_bar = display.getHeight() - v.getBottom();
Run Code Online (Sandbox Code Playgroud)

它对于设备2和3是正确的.我不确定设备1.用户向我发送了设备1的屏幕截图.那里有一个带有软件按钮的状态栏.但表达式"display.getHeight() - v.getBottom()"给出0.


LKa*_*ipo 7

您还可以使用此博客文章描述的方式获取android的dimens.xml文件中找到的状态栏的维度.

这可以获得状态栏的高度,而无需等待绘图.

从博客文章中引用代码:

public int getStatusBarHeight() {
  int result = 0;
  int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
  if (resourceId > 0) {
      result = getResources().getDimensionPixelSize(resourceId);
  }
  return result;
}
Run Code Online (Sandbox Code Playgroud)

您需要将此方法放在ContextWrapper类中.


Kac*_*chi 5

在onCreate方法中将runnable附加到您的一个视图,并将上面的代码放在那里.这将导致应用程序在连接到屏幕时计算状态栏+标题屏幕高度.

看看下面的代码:

myView.post(new Runnable() {

        @Override
        public void run() {
            Rect rectgle= new Rect();
            Window window= getWindow();
            window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
            int StatusBarHeight= rectgle.top;
            int contentViewTop= window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
            int TitleBarHeight= contentViewTop - StatusBarHeight;

        }

    });
Run Code Online (Sandbox Code Playgroud)

如果仍然没有做到这一点,请尝试调用视图的postDelayed方法而不是post并添加毫秒值作为第二个参数.