Android中懸浮窗口的實(shí)現(xiàn)原理實(shí)例分析
本文實(shí)例講述了Android中懸浮窗口的實(shí)現(xiàn)原理。分享給大家供大家參考。具體如下:
用了我一個(gè)周末的時(shí)間,個(gè)中憤懣就不說(shuō)了,就這個(gè)問(wèn)題,我翻遍全球網(wǎng)絡(luò)沒(méi)有一篇像樣的資料,現(xiàn)在將實(shí)現(xiàn)原理簡(jiǎn)單敘述如下:
調(diào)用WindowManager,并設(shè)置WindowManager.LayoutParams的相關(guān)屬性,通過(guò)WindowManager的addView方法創(chuàng)建View,這樣產(chǎn)生出來(lái)的View根據(jù)WindowManager.LayoutParams屬性不同,效果也就不同了。比如創(chuàng)建系統(tǒng)頂級(jí)窗口,實(shí)現(xiàn)懸浮窗口效果!
WindowManager的方法很簡(jiǎn)單,基本用到的就三個(gè)addView,removeView,updateViewLayout。
而WindowManager.LayoutParams的屬性就多了,非常豐富,具體請(qǐng)查看SDK文檔。這里給出Android中的WindowManager.java源碼,可以具體看一下。
下面是簡(jiǎn)單示例代碼:
public class myFloatView extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button bb=new Button(getApplicationContext()); WindowManager wm=(WindowManager)getApplicationContext().getSystemService("window"); WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams(); /** *以下都是WindowManager.LayoutParams的相關(guān)屬性 * 具體用途請(qǐng)參考SDK文檔 */ wmParams.type=2002; //這里是關(guān)鍵,你也可以試試2003 wmParams.format=1; /** *這里的flags也很關(guān)鍵 *代碼實(shí)際是wmParams.flags |= FLAG_NOT_FOCUSABLE; *40的由來(lái)是wmParams的默認(rèn)屬性(32)+ FLAG_NOT_FOCUSABLE(8) */ wmParams.flags=40; wmParams.width=40; wmParams.height=40; wm.addView(bb, wmParams);//創(chuàng)建View } }
別忘了在AndroidManifest.xml中添加權(quán)限:
PS:這里舉例說(shuō)明一下type的值的意思:
/** * Window type: phone. These are non-application windows providing * user interaction with the phone (in particular incoming calls). * These windows are normally placed above all applications, but behind * the status bar. */ public static final int TYPE_PHONE = FIRST_SYSTEM_WINDOW+2; /** * Window type: system window, such as low power alert. These windows * are always on top of application windows. */ public static final int TYPE_SYSTEM_ALERT = FIRST_SYSTEM_WINDOW+3;
這個(gè)FIRST_SYSTEM_WINDOW的值就是2000。2003和2002的區(qū)別就在于2003類(lèi)型的View比2002類(lèi)型的還要top,能顯示在系統(tǒng)下拉狀態(tài)欄之上!
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
解決Android Studio突然不顯示logcat日志的問(wèn)題
這篇文章主要介紹了解決Android Studio突然不顯示logcat日志的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04Android提高之SQLite分頁(yè)表格實(shí)現(xiàn)方法
這篇文章主要介紹了Android提高之SQLite分頁(yè)表格實(shí)現(xiàn)方法,在項(xiàng)目開(kāi)發(fā)中有很高的實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08android 電話(huà)狀態(tài)監(jiān)聽(tīng)(來(lái)電和去電)實(shí)現(xiàn)代碼
從事android開(kāi)發(fā)的朋友們可能電話(huà)狀態(tài)監(jiān)聽(tīng)不是很擅長(zhǎng),接下來(lái)將詳細(xì)介紹電話(huà)狀態(tài)監(jiān)聽(tīng)功能的實(shí)現(xiàn)步驟,需要了解的朋友可以參考下2012-12-12另外兩種Android沉浸式狀態(tài)欄實(shí)現(xiàn)思路
這篇文章主要為大家介紹了另外兩種Android沉浸式狀態(tài)欄實(shí)現(xiàn)思路,android5.0及以后版本都支持給狀態(tài)欄著色,而目前android主流版本還是4.4,想要深入了解的朋友可以參考一下2016-01-01Android init.rc文件詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android init.rc文件詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01Android ListView用EditText實(shí)現(xiàn)搜索功能效果
本篇文章主要介紹了Android ListView用EditText實(shí)現(xiàn)搜索功能效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Android 實(shí)現(xiàn)帶字母索引的側(cè)邊欄功能
這篇文章主要介紹了Android 實(shí)現(xiàn)帶字母索引的側(cè)邊欄功能,需要的朋友可以參考下2017-08-08Android 使用Glide加載網(wǎng)絡(luò)圖片等比例縮放的實(shí)現(xiàn)方法
這篇文章主要介紹了Android 使用Glide加載網(wǎng)絡(luò)圖片等比例縮放的實(shí)現(xiàn)方法,需要的朋友可以參考下2018-08-08Android自定義wheelview隨機(jī)選號(hào)效果
這篇文章主要介紹了Android自定義wheelview隨機(jī)選號(hào)效果,利用wheelview實(shí)現(xiàn)滾動(dòng)隨機(jī)選擇號(hào)碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12