显示状态栏上方的视图?

时间:2012-03-22 03:09:41

标签: android eclipse view statusbar

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

我知道你可以从状态栏的正下方获得一个视图,该视图位于父对齐的顶部。但是,如何在状态栏上查看视图?

Example

4 个答案:

答案 0 :(得分:28)

禁用系统状态栏 - 无根


经过两天的搜索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);

不要忘记权限:

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

注意:
 测试了kitkat。

答案 1 :(得分:12)

@Sadeshkumar的答案对于ICS及以上(也许是GB)是不正确的。

TYPE_SYSTEM_ALERT涵盖了使用FLAG_LAYOUT_IN_SCREENStatusBar创建的视图。

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

问题在于,如何获得点击/触摸?

答案 2 :(得分:6)

TYPE_SYSTEM_ERROR涵盖了使用FLAG_LAYOUT_IN_SCREENStatusBar创建的视图。

答案 3 :(得分:4)

 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);  
相关问题