最近同事裝了個天氣通,發現它的晴天特效很炫啊,而且在系統介面上也會出現,覺得很不錯,特研究了下,其實很簡單,就是改下WindowMananger的屬性就可以了,然後用WindowManager添加要顯示的介面就OK了,代碼如下:
public class PopupActivity extends Activity {@Overrideprotected 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"/>
發現很多比較不錯的功能,實現其實並不複雜,關鍵在對每個介面的瞭解程度