Android開發隨手記–系統懸浮框

來源:互聯網
上載者:User

最近同事裝了個天氣通,發現它的晴天特效很炫啊,而且在系統介面上也會出現,覺得很不錯,特研究了下,其實很簡單,就是改下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"/>

發現很多比較不錯的功能,實現其實並不複雜,關鍵在對每個介面的瞭解程度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.