在很多音乐播放器中都会出现歌词的置顶,这种置顶是对于整个系统而言的。
实现方式
WindowManager wm = (WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE);//获得WindowManager WindowManager.LayoutParams params = new WindowManager.LayoutParams(); //布局参数对象 params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; //system overlay windows, which need to be displayed on top of everything else. params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE; //可选的。使得这个view失去焦点,后面的view可获得焦点。
TextView tv = new TextView(this); wm.addView(tv, params);
记得要在Manifest添加权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>