Android 懸浮表單問題

來源:互聯網
上載者:User

通過WindowManager的addView方法建立View,這樣產生出來的View根據WindowManager.LayoutParams屬性不同,效果也就不同了。
比如建立系統最上層視窗,實現懸浮視窗效果!

WindowManager wm = getWindow().getWindowManager();
TextView bb = new TextView();
WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
wmParams.type=WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG; 
wmParams.format=PixelFormat.TRANSLUCENT;
wmParams.width=WindowManager.LayoutParams.MATCH_PARENT;
wmParams.height=WindowManager.LayoutParams.WRAP_CONTENT;

try{
wm.addView(bb, wmParams);  //

} catch(Exception e) {
//TODO
}

備忘:
TYPE_APPLICATION_ATTACHED_DIALOG Window type: like TYPE_APPLICATION_PANEL, but layout of the window happens as that of a top-level window, not as a child of its container.
如上就可以添加一個懸浮的表單,但是會存在這樣一個問題,只有bb可以接受事件,但其後的一些空間如有觸發點擊事件則無法做到。分析原因,可能是通過WindowManager的
addView方法添加的view預設擷取焦點,並且無法轉移。
通過查閱資料,發現在WindowManager的LayoutParams屬性中,有一個flag屬性可以設定其相關的擷取焦點狀態。

FLAG_NOT_TOUCH_MODAL Window flag: Even when this window is focusable (its {@link #FLAG_NOT_FOCUSABLE is not set), allow any pointer events outside of the window to be sent to the windows behind it.

重新設定WindowManager.LayoutParams的相關屬性。具體如下:

     WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT);
     params.gravity = Gravity.TOP;
       
然後,通過WindowManager的addView方法,重新把bb添加到螢幕上去就OK了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.