最近同事装了个天气通,发现它的晴天特效很炫啊,而且在系统界面上也会出现,觉得很不错,特研究了下,其实很简单,就是改下WindowMananger的属性就可以了,然后用WindowManager添加要显示的界面就OK了,代码如下:
public class PopupActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button test = new Button(this);
test.setText("Test");
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams lp = new LayoutParams();
//关键属性
lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
lp.format = 1;
lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
lp.flags = lp.flags | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
lp.flags = lp.flags | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
lp.width = 140;
lp.height = 140;
wm.addView(test, lp);
}
}
同时别忘了在AndroidManifest.xml里添加权限:<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
发现很多比较不错的功能,实现其实并不复杂,关键在对每个接口的了解程度