eclipse 在状态栏上方显示视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9815901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 17:51:39  来源:igfitidea点击:

Display view above status bar?

androideclipseviewstatusbar

提问by user1190019

I recently saw an image of an app that was capable of displaying a view above the status bar and was also able to cover it with a view.

我最近看到了一个应用程序的图像,它能够在状态栏上方显示一个视图,也能够用一个视图覆盖它。

I know you can get a view right below the status bar from a view with align parent top. But how would you get a view on top of the status bar??

我知道您可以从对齐父顶部的视图中获得状态栏正下方的视图。但是如何在状态栏顶部获得视图?

Example

例子

回答by amalBit

Disable the System Status Bar - Without Root

禁用系统状态栏 - 无 Root



After two full days of searching through SO posts and reading the Android docs over and over.. Here is the solution that I came up with. (tested)

经过整整两天搜索SO帖子并一遍又一遍地阅读Android文档..这是我想出的解决方案。(已测试)

    mView= new TextView(this);                                       
    mView.setText(".........................................................................");                       

    mLP = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            100,
            // Allows the view to be on top of the StatusBar
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            // Keeps the button presses from going to the background window
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
            // Enables the notification to recieve touch events
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
            // Draws over status bar
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
            PixelFormat.TRANSLUCENT);

    mLP.gravity =  Gravity.TOP|Gravity.CENTER;      

    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    mWindowManager.addView(mView, mLP);

Dont forget the permissions:

不要忘记权限:

<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

Note:
Tested upto kitkat.

注意:
已测试到 kitkat。

回答by logray

The answer by @Sadeshkumar is incorrect for ICS and above (perhaps GB as well).

@Sadeshkumar 的答案对于 ICS 及更高版本(可能也是 GB)是不正确的。

A view created with TYPE_SYSTEM_ALERTand FLAG_LAYOUT_IN_SCREENis covered by the StatusBar.

使用TYPE_SYSTEM_ALERT和创建的视图FLAG_LAYOUT_IN_SCREEN被 覆盖StatusBar

To get an overlay on top of the StatusBar, you need to use TYPE_SYSTEM_OVERLAYinstead of TYPE_SYSTEM_ALERT.

要在 之上获得叠加层StatusBar,您需要使用TYPE_SYSTEM_OVERLAY代替TYPE_SYSTEM_ALERT

The problem being then, how to get clicks/touches?

那么问题来了,如何获得点击/触摸?

回答by Will

A view created with TYPE_SYSTEM_ERRORand FLAG_LAYOUT_IN_SCREENis covered by the StatusBar.

使用TYPE_SYSTEM_ERROR和创建的视图FLAG_LAYOUT_IN_SCREEN被 覆盖StatusBar

回答by Sadeshkumar Periyasamy

 int statusBarHeight = (int) Math.ceil(25 * getResources().getDisplayMetrics().density);


  View statusBarView = new View(MyActivity.this);
  statusBarView.setBackgroundColor(Color.GREEN);


  WindowManager.LayoutParams params = null;
  params = new WindowManager.LayoutParams(WindowManager.LayoutParams.FILL_PARENT,statusBarHeight,WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.RIGHT | Gravity.TOP;
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    wm.addView(statusBarView, params);