【问题标题】:Android add view in status barAndroid在状态栏中添加视图
【发布时间】:2012-11-16 18:48:43
【问题描述】:

这是我添加视图的代码:

        LayoutInflater layoutInflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView tv = (TextView) layoutInflater.inflate(R.layout.textview, null);
    final View view = tv;
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, 25,
            WindowManager.LayoutParams.TYPE_STATUS_BAR,
            WindowManager.LayoutParams.FLAG_SCALED
            , PixelFormat.TRANSLUCENT);


    WindowManager wm = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
    wm.addView(view, lp);

但它会给我一个错误:

android.view.WindowManager$BadTokenException: 无法添加窗口 android.view.ViewRootImpl$W@41668948 -- 此窗口类型的权限被拒绝

所以我在 android manifast 中添加权限

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

但还是有错误,我需要帮助!!

【问题讨论】:

    标签: android notifications statusbar


    【解决方案1】:

    为了在状态栏顶部绘制视图,这是在阅读了这么多堆栈溢出帖子后对我有用的组合:

    1- 您需要在您的服务或活动上拥有此 LayoutParameters 的 WindowManager:

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.MATCH_PARENT, 
        statusBarHeight, 
        WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |       
        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | 
        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | 
        WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        PixelFormat.TRANSLUCENT);
    

    2- 您只需要在您的 android 清单上获得此权限:

    uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"
    

    希望对您有所帮助。就我而言,我只需要用半透明的黄色或绿色为状态栏着色,以表明用户当前正在通话中

    【讨论】:

    • 很好,但是在此窗口管理器上未启用点击,我尝试更改标志但我无法让它听点击(之前使用不同的标志工作正常),有没有解决方案为此...我在该区域显示自定义祝酒词
    【解决方案2】:

    从代码 sn-p 来看,就像您正在尝试创建一个新的 状态栏窗口, 这是不可能的。 (有一个状态栏,由系统 UI 管理。)

    你能描述一下你想要做什么吗?通常,您使用NotificationNotificationManager API 与状态栏进行交互。

    【讨论】:

      【解决方案3】:

      好的,这应该可以工作:

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

      【讨论】:

      • 您还有其他想法可能是什么问题吗?
      • 你可以包括一个堆栈跟踪
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多