標籤:android style http ar os 使用 sp java strong
ListPopupWindow ListPopupWindow簡述
ListPopupWindow最低要求為api11,為了相容到2.1, 可以使用包含在support V7包中實現。
從效果上來講,ListPopupWindow就是一個彈出層的ListView,比較適合用來實現自訂的下拉式功能表以及自訂的下拉挑選清單。
ListPopupWindow的使用 自訂樣式
一個樣本:
<style name="V7.ListPopupWindowStyle" parent="@style/Widget.AppCompat.ListPopupWindow"> <item name="android:popupBackground">#404040</item> //彈出層的背景 <item name="android:dropDownVerticalOffset">0dip</item> <item name="android:dropDownHorizontalOffset">0dip</item> //水平以及垂直位移 <item name="android:dropDownWidth">match_parent</item> //這個效果不大</style><style name="V7.DropDownListViewStyle" parent="@style/Widget.AppCompat.ListView.DropDown"> <item name="android:listSelector">@drawable/list_selector</item> // <item name="android:divider">#242424</item> <item name="android:dividerHeight">1px</item> ... //其他列表樣式tyle>AppTheme是應用到Activity的主題listPopupWindowStyle 對應彈出層的主題樣式dropDownListViewStyle 對應內含列表的主題樣式,與普通ListView的定製方式一致<style name="V7.ListPopupWindow" parent="AppTheme"> <item name="listPopupWindowStyle">@style/V7.ListPopupWindowStyle</item> <item name="dropDownListViewStyle">@style/V7.DropDownListViewStyle</item></style>
代碼調用
實現右上方快顯功能表,使用方式與PopupWindow差不多:
ListPopupWindow listPopupWindow = new ListPopupWindow(this);listPopupWindow.setAnchorView(view);listPopupWindow.setWidth(300); //如果不設定寬度的話,預設取AnchorView的寬度,一般不是我們想要的結果listPopupWindow.setModal(true); //是否為模態,影響到對back按鈕的處理listPopupWindow.setAdapter(new ArrayAdapter<String>(this, R.layout.apt_v7_list_popup_window, R.id.apt_v7_tv, new String[]{ "發起群聊", "添加朋友", "掃一掃", "意見反饋"}));listPopupWindow.show(); 與PopMenu的對比
- PopMenu難以定製,ListPopupWindow的定製性更好
- ListPopupWindow不能自適應寬度
- PopMenu以面向菜單為核心,可以更方便的實現 禁用/開啟 功能
一個讓ListPopupWindow自適應寬度的方案,設定adapter後,檢測每一行的最大寬度,然後再來設定 ListPopupWindow 的寬度,有利有弊,自己取捨了。
關於菜單那的其他實現方式:
PopMenu
PopupWindow + 自訂ContentView
頁面內View + 自訂touch事件以及按鍵事件處理
demo
demo
Android分享 Q群:315658668
【Android】ListPopupWindow