【Android】ListPopupWindow

來源:互聯網
上載者:User

標籤: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的對比
  1. PopMenu難以定製,ListPopupWindow的定製性更好
  2. ListPopupWindow不能自適應寬度
  3. PopMenu以面向菜單為核心,可以更方便的實現 禁用/開啟 功能

一個讓ListPopupWindow自適應寬度的方案,設定adapter後,檢測每一行的最大寬度,然後再來設定 ListPopupWindow 的寬度,有利有弊,自己取捨了。

關於菜單那的其他實現方式:

  1. PopMenu

  2. PopupWindow + 自訂ContentView

  3. 頁面內View + 自訂touch事件以及按鍵事件處理

demo

demo

Android分享 Q群:315658668

【Android】ListPopupWindow

聯繫我們

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