android 實現懸浮控制項,android懸浮控制項
實現案頭View 如案頭歌詞
1)將要顯示在案頭的view,通過WindowManager.addView,掛在到WindowManager下;注意,
WindowManager對象來源,源碼有解釋。 並且配置好view的WindowManager.LayoutParams
1-1)WindowManager.LayoutParams.type 設定為 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;時,案頭UI不可以直接setOnClickListener(),該View的onTouchEvent()函數也無效了 設定為WindowManager.LayoutParams.TYPE_SYSTEM_ALERT(2003)或者WindowManager.LayoutParams.TYPE_PHONE(2002)才有效。
1-2)WindowManager.LayoutParams.flag 設定為LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE
如果設定了WindowManager.LayoutParams.type為2002,2003;那麼flag不設定為這兩個flag的話,那麼其他組件就無法獲得touch和focus事件了。
設定了該兩個屬性之後就大家都可以獲得Touch和Focus事件
2)androidManifest裡申明許可權
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
private void initDestopText(View childView){//直接通過Activity擷取的 WindowManager,在act退出時,案頭組件也將退出。//WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE); //一定要通過getApplicationContext()擷取WindowManager,這種情況下,當Application終止後,懸浮控制項才會被退出WindowManager wm = (WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE); WindowManager.LayoutParams params = new WindowManager.LayoutParams(); //params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; //如果設定為params.type = WindowManager.LayoutParams.TYPE_PHONE; // params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE;params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.height = WindowManager.LayoutParams.WRAP_CONTENT; wm.addView(childView, params); }
Android懸浮控制項怎實現?
樓主說的應該是類似現在許多通訊錄軟體右側的字母索引捲軸那種效果吧? 到安卓巴士網站查看回答詳情>>
android中實現動態插入控制項的問題
直接給你上代碼吧,寫了我半個小時,經過了我的測試了的~
運行下就能看到結果了~關鍵的remove的時候有給你寫注釋~
布局的layout檔案內容:
----------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/linearlayout">
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:layout_height="wrap_content" android:id="@+id/add"
android:text="Add" android:layout_width="100px"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="100px" android:text="Remove" android:id="@+id/remove"></Button>
</LinearLayout>
<TextView android:id="@+id/TextView01" android:text="This is textView."
android:layout_width="fill_parent" android:gravity="center"
android:layout_height="50px"></TextView>
</LinearLayout>
-----------------------------------------------------------......餘下全文>>