private void createFloatView() { wmParams = new WindowManager.LayoutParams(); mWindowManager = (WindowManager) getApplication().getSystemService( Context.WINDOW_SERVICE); wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR; // 设置图片格式,效果为背景透明 (否则为黑色) wmParams.format = PixelFormat.RGBA_8888; // 注意此处的模式 (该FLAG节点下的悬浮窗全都是置于其他应有的上层位置的) wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;//FLAG_NOT_FOCUSABLE //wmParams.flags = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR; wmParams.gravity = Gravity.BOTTOM | Gravity.CENTER; wmParams.x = 0; wmParams.y = 0; // 设置悬浮窗口长宽数据 wmParams.width = Integer.valueOf((int) (getDisplaysMetrics()[1] * 1)); wmParams.height = 122; LayoutInflater inflater = LayoutInflater.from(AppApplication.appContext); // 获取浮动窗口视图所在布局 mFloatLayout = (RelativeLayout) inflater.inflate(R.layout.floatview_main, null); // 添加mFloatLayout mWindowManager.addView(mFloatLayout, wmParams); // 浮动窗口按钮 (布局没有加载完毕之前,获取不到高度。) LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT); params.gravity = Gravity.CENTER; speechMicLinearLayout = (RelativeLayout) mFloatLayout.findViewById(R.id.speech_mic_linear); //speechMicLinearLayout.addView(waveView, params); mFloatLayout.setVisibility(View.VISIBLE);//INVISIBLE textView = (TextView) mFloatLayout.findViewById(R.id.OutputText); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.w("TAGE", "onClick.................."); } }); }
说明一点比较重要的知识点:
悬浮窗一旦开启都是置于其他应用的上层的,无法置于某个应用的下层
置于悬浮窗的透明度可以通过设置主题进行更改/github上边有一个开源比较好用的悬浮窗,可自行上git搜索