如何在Android中创建位于所有应用程序之上的UI或窗口小部件?

时间:2012-07-16 05:03:37

标签: android android-windowmanager

我可以在Android中创建一个位于所有应用程序之上的UI或小部件吗?有些应用程序有这样的小部件。一个示例在所有应用程序的顶部都有一个摄像头图标,单击该图标时将捕获屏幕。

2 个答案:

答案 0 :(得分:18)

如果您只想显示某些内容,即使是锁屏也可以显示在所有内容上。

如果您想要点击某些内容,可以将其显示在除锁定屏幕之外的任何内容上。

以下是一个示例,根据您的需求进行修改:

创建服务并执行以下操作:

//These three are our main components.
WindowManager wm;
LinearLayout ll;
WindowManager.LayoutParams ll_lp;

//Just a sample layout parameters.
ll_lp = new WindowManager.LayoutParams();
ll_lp.format = PixelFormat.TRANSLUCENT;
ll_lp.height = WindowManager.LayoutParams.FILL_PARENT;
ll_lp.width = WindowManager.LayoutParams.FILL_PARENT;
ll_lp.gravity = Gravity.CLIP_HORIZONTAL | Gravity.TOP;

//This one is necessary.
ll_lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

//Play around with these two.
ll_lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
ll_lp.flags = ll_lp.flags | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

//This is our main layout.
ll = new LinearLayout(this);
ll.setBackgroundColor(android.graphics.Color.argb(0, 0, 0, 0));
ll.setHapticFeedbackEnabled(true);

//And finally we add what we created to the screen.
wm.addView(ll, ll_lp);

答案 1 :(得分:3)

以下是有关视图显示方式的更多选项。

这将使其成为所有内容(包括锁定屏幕)的叠加层,但不会点击。     的 WindowManager.LayoutParams。 TYPE_SYSTEM_OVERLAY

这将使其可以点击,但它不会超过锁定屏幕     的 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT

这将使其高于一切(包括锁定屏幕)和可点击。     的 WindowManager.LayoutParams.TYPE_SYSTEM_ERROR

关于使用“TYPE_SYSTEM_ERROR”需要注意的一件事。如果你点击一个点击事件,它调用的任何东西都会发生在锁定屏幕后面。