PopupWindow 使用詳解(一) 中文API 文檔 贈送 ListPopupWindow 中文 API

筆者數了數枕巾脫落的頭髮,發現了 Android 開發經常使用的相似於彈窗的懸浮窗口 大概是 PopupWindow 和 dialog。以前有還算具體的寫過關於 dialog 以及自定義 dialog 甚至是 dialogActivity 的相關筆記,即:Android 系統原生dialog使用Android dialog Activity 使用android自定義相對複雜dialog。忽然感受 PopupWindow 這個說簡單不簡單,說難不難的知識點也應該詳細研究一下。
下面迴歸正題,請欣賞筆者的表演:android

1、建立 PopupWindow

先說一下 Google 爸爸提供的構造方法git

public PopupWindow() 

public PopupWindow(View contentView) 
public PopupWindow(int width, int height)
public PopupWindow(View contentView, int width, int height)
public PopupWindow(View contentView, int width, int height, boolean focusable)

public PopupWindow(Context context)
public PopupWindow(Context context, AttributeSet attrs)
public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr)
public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

數一數很少很多 9 個構造方法,一個無參的 8 個有參的,參數表明的意思分別爲:
View contentView表示該 PopupWindow 內裝載的內容,即展現各位用戶的內容
int width, int height表示該 PopupWindow 對象的長度和寬度,參數能夠是 ViewGroup.LayoutParams.WRAP_CONTENTViewGroup.LayoutParams.MATCH_PARENT
也能夠是具體的數值了。可是 這裏規定的 是 PopupWindow 內加載到的 view 對象的大小。至關於 PopupWindow 設置的值 爲 xml 內最外層佈局是等效的。github

若是,須要控制加載展現的 view 大小,那麼須要將該參數設置ViewGroup.LayoutParams.WRAP_CONTENT而且 目標樣式外再包裹一層佈局,相似於:app

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true">

        <ImageView
            android:id="@+id/iv_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/app_name"
            android:src="@mipmap/home" />
    </RelativeLayout>
</RelativeLayout>

boolean focusablePopupWindow 響應內部的點擊事件,例如 TextView、Button 等
Context context上下文,很少講
剩下下面的三個構造方法,不經常使用,可是會自定義 view 的大佬都知道啥意思。ide

2、經常使用設置方法

//設置動畫特效 即 展現和消失動畫
public void setAnimationStyle(int animationStyle)
//主要做用是爲了設置 PopupWindow 顯示的時候是否會與 StatusBar 重疊(若是存在的話也包括 SystemBar )
public void setAttachedInDecor(boolean enabled)
//設置  PopupWindow 的背景。該屬性不設置的會,會致使 PopupWindow 出現後不會消失,即使是 點擊 back 鍵也不起做用。這應該是 PopupWindow 較爲變態的地方。
public void setBackgroundDrawable(Drawable background)
//設置 PopupWindow 容許超出窗口
public void setClippingEnabled(boolean enabled)
//設置 PopupWindow  內展現的內容
public void setContentView(View contentView)
//設置 PopupWindow  的高度,相似於 3D 效果的陰影
public void setElevation(float elevation)
//設置 PopupWindow的入場動畫
public void setEnterTransition(Transition enterTransition) 
//有如就得有出 設置出場動畫
public void setExitTransition(Transition exitTransition)
//設置 popupWindow 是否能夠獲取焦點
 public void setFocusable(boolean focusable)
//設置 PopupWindow的高度
 public void setHeight(int height) 
//臉頰事件  Events 都是有大小的當觸摸點大於手指頭大小時,則爲 臉頰事件 ,蠻有意思的 你能夠嘗試一下。
 public void setIgnoreCheekPress()
//設置輸入法的操做模式 
 public void setInputMethodMode(int mode)
//監聽 PopupWindow關閉的事件
 public void setOnDismissListener(OnDismissListener onDismissListener)
//設置 點擊 PopupWindow意外區域,隱藏 popupWindow  然而並無什麼卵用
 public void setOutsideTouchable(boolean touchable) 
// PopupWindow觸摸時的監聽回調
 public void setTouchInterceptor(OnTouchListener l)
//設置 PopupWindow是否可觸摸
 public void setTouchable(boolean touchable)
//設置 popwindow 的寬度
 public void setWidth(int width)
//設置 PopupWindow佈局類型
  public void setWindowLayoutType(int layoutType)

設置 PopupWindow 顯示位置 相對重要 拿出來寫工具

//設置 PopupWindow 在某個控件的下方,某控件的左下角與 PopupWindow 的左上角對齊
public void showAsDropDown(View anchor)

//設置 PopupWindow 在某個控件的下方,某控件的左下角與 PopupWindow 的左上角對齊,能夠設置偏移量 向左爲副 ,向右爲正;向上爲負,向下爲正
 public void showAsDropDown(View anchor, int xoff, int yoff) 
//設置 PopupWindow 在至關於父佈局進行擺放,能夠設置偏移量。
public void showAtLocation(View parent, int gravity, int x, int y)
//隱藏 PopupWindow 
public void dismiss()

3、經常使用獲取方法

//獲取 PopupWindow 動畫樣式
 public int getAnimationStyle()
//獲取 PopupWindow 背景
 public Drawable getBackground() 
//獲取 PopupWindow 設置的 view 返回一個view對象
 public View getContentView() 
//得到 PopupWindow 的懸浮高度
 public float getElevation() 
//獲取 PopUpWindow 的入場動畫
 public float getElevation() 
//獲取 PopupWindow 的出場動畫
public Transition getExitTransition()
//獲取 PopupWindow 的高度
public int getHeight() 
//獲取輸入模式
public int getInputMethodMode() 
//獲取 PopupWindow 是否應該與寄生(不太合理可是不知道用啥詞)對象重疊
public boolean getOverlapAnchor()
//獲取 Popwindow 的寬度
 public int getWidth()
//獲取 PopupWindow 的佈局類型
 public int getWindowLayoutType() 

//獲取可讓 PopupWindow 設置的最大高度
public int getMaxAvailableHeight(View anchor)
public int getMaxAvailableHeight(View anchor, int yOffset) 
//獲取能夠設置 PopupWindow 的最大高度,能夠忽略軟鍵盤。
public int getMaxAvailableHeight(
            View anchor, int yOffset, boolean ignoreBottomDecorations)

4、經常使用判斷方法

//判斷 PopupWindow 是否在超出父佈局
public boolean isAboveAnchor() 
// 判斷 PopupWindow 是否在父佈局的 裝飾上
public boolean isAttachedInDecor()
//判讀 PopupWindow 是否能夠超出窗口
public boolean isClippingEnabled()
//判斷 PopupWindow 是否能夠獲取焦點
public boolean isFocusable()
//判斷 PopupWindow 是否相應自身之外的點擊事件
public boolean isOutsideTouchable()
//判斷 PopupWindow 是否展現
public boolean isShowing()
//判斷 PopupWindow 是否支持多點觸控
public boolean isSplitTouchEnabled()
//判斷 PopupWindow 是否支持觸摸事件
public boolean isTouchable()

5、不常使用 更新位置或更新內容方法

//更新當前 PopupWindow
public void update() 
//更新 Popwindow 到新的形態  參數:長度、寬度
public void update(int width, int height) 
//更新 Popwindow 到新的位置、新的形態  參數:新位置的座標,長度、寬度
public void update(int x, int y, int width, int height)
//同上
public void update(int x, int y, int width, int height, boolean force)

//相對於宿主 更新體型
public void update(View anchor, int width, int height)
//相對宿主更新 大小 位置
public void update(View anchor, int xoff, int yoff, int width, int height)

6、部分主要事項

一、

PopupWindow 默認是不容許超出窗口的,具體演示請看下篇筆記;佈局

二、

PopupWindow 的 public void setOutsideTouchable(boolean touchable)方法默認沒有什麼卵用 ,是否能夠點擊 PopupWindow 外部進行隱藏 PopupWindow ,必須設置 public void setBackgroundDrawable(Drawable background)該方法post

三、

getHeight()和 getWidth()方法不太好用,通常返回爲 0;若是獲取 PopupWindow 的長寬建議使用以下方法:學習

devPop.getContentView().measure(0, 0);
int hight=devPop.getContentView().getMeasuredHeight();
int width=devPop.getContentView().getMeasuredWidth();

四、

將 PopupWindow 的四角設置爲圓角方法,能夠將事先準備的 view 的父佈局加一個帶有圓角的 shape background, 以後設置 PopupWindow 的 setBackgroundDrawable() 方法 設置爲devPop.setBackgroundDrawable(new ColorDrawable(0x00ffffff));動畫

五、

必定要在顯示 PopupWindow 的界面的 OnDestroy()方法內調用 dismiss()方法,不然會產生 「意想不到」的驚喜。

@Override
    protected void onDestroy() {
        super.onDestroy();
        if (popupWindow != null) {
            popupWindow.dismiss();
        }
    }

六、

筆者遇到的注意事項,感受目前就這麼多了,以後若是遇到還會補充;

7、附贈 ListPopupWindow 相關漢語 API

一、效果展現

或許各位不少人都知道 PopupWindow 可是 少有人知道 ListPopupWindow ,故 展現效果以下,具體應用 請留意以後的 筆記。

ListPopupWindow 效果

二、相關 API

繼承結構

 

繼承結構

(1)、構造方法

public ListPopupWindow( Context context)
public ListPopupWindow( Context context,  AttributeSet attrs)
public ListPopupWindow( Context context,  AttributeSet attrs,int defStyleAttr) 
public ListPopupWindow(Context context,  AttributeSet attrs,int defStyleAttr,  int defStyleRes)

構造方法就這四種,筆者經常使用的是第一種。

(2)、設置類方法

//爲列表設置 適配器
public void setAdapter( ListAdapter adapter)
//設置錨點 view 寄生的view
public void setAnchorView(@Nullable View anchor)
//爲 ListPopupWindow 設置動畫樣式
public void setAnimationStyle( int animationStyle) 
//爲 ListPopupWindow 設置背景
public void setBackgroundDrawable( Drawable d)
//設置 ListPopupWindow 的寬度 單位:像素
public void setContentWidth(int width)
//設置下拉列表的對齊方式
public void setDropDownGravity(int gravity)
//設置 ListPopupWindow 的高度 單位:像素
public void setHeight(int height)
//設置橫向偏移量 單位:像素
public void setHorizontalOffset(int offset)
//設置 Drawable 爲列表的選擇器
public void setListSelector(Drawable selector) 
//將 ListPopupWindow 設置爲模態框
 public void setModal(boolean modal)
//設置 ListPopupWindow  提示位置
public void setPromptView( View prompt)
//設置 ListPopupWindow  的選定位置
public void setSelection(int position)
//設置 ListPopupWindow  輸入區域的輸入模式
public void setSoftInputMode(int mode)
//設置 ListPopupWindow  的豎直方向偏移量
public void setVerticalOffset(int offset)
//設置 ListPopupWindow  的寬度
public void setWidth(int width) 
//設置 ListPopupWindow  的佈局類型
public void setWindowLayoutType(int layoutType)
// 可將返回對象 添加到 源視圖中,進而實現拖動並打開。
public OnTouchListener createDragToOpenListener(View src)

//展現 ListPopupWindow  
public void show()

(3)、監聽器

//設置 ListPopupWindow  關閉的 時的監聽
public void setOnDismissListener(PopupWindow.OnDismissListener listener) 
//設置 ListPopupWindow 的 item 單擊時的監聽回調
public void setOnItemClickListener(AdapterView.OnItemClickListener clickListener)
//設置 ListPopupWindow  item 被選擇 時的監聽回調
public void setOnItemSelectedListener(OnItemSelectedListener selectedListener)

(4)、獲取方法

//獲取 ListPopupWindow  的錨點 view
public View getAnchorView()
//獲取 ListPopupWindow  的動畫樣式
public  int getAnimationStyle() 
//獲取 ListPopupWindow  的背景
public  Drawable getBackground() 
//獲取 ListPopupWindow 的高度
public int getHeight()
//獲取 ListPopupWindow 的橫向偏移量
public int getHorizontalOffset() 
//獲取 ListPopupWindow 的 列表
public  ListView getListView()
//獲取可選的 ListPopupWindow 的位置
public int getPromptPosition()
//獲取 ListPopupWindow 內被選中的 item
public  Object getSelectedItem() 
//獲取被選中的 item 的 id
public long getSelectedItemId()
//獲取被選中的 item 的位置
public int getSelectedItemPosition() 
//獲取被選中的 view
public  View getSelectedView() 
//獲取 垂直方向的偏移量  單位:像素
public int getVerticalOffset() 
//獲取 ListPopupWindow 的寬度
public int getWidth()

(5)、判斷方法

//判斷是否爲 模態框樣式
public boolean isModal()
//判斷是否爲顯示狀態
public boolean isShowing()

//過了關鍵按鍵  點擊事件
public boolean onKeyDown(int keyCode,  KeyEvent event)
//過濾關鍵按鍵 點擊擡起事件
public boolean onKeyUp(int keyCode,  KeyEvent event)
//判斷 是否 在指定 item 下執行點擊事件
public boolean performItemClick(int position)

(6)、其餘的重要方法

//執行相關 對 ui 線程的操做
public void postShow()
//刪除 當前列表的全部選擇
public void clearListSelection() 
//關閉 ListPopupWindow 
public void dismiss()

三、關於 ListPopupWindow 的其它 雜談

當發現這個 ListPopupWindow 算是控件的工具值,筆者捨去不少地方的 ListView 和部分AutoCompleteTextView 和 Spinner 控件使用,由於 ListPopupWindow 搭配 TextView 或者搭配 EditText 極其好用。

今天學習就到這裏,筆者但願能夠幫助各位在夥伴,歡迎大佬指點、批評還有建議;
以後的幾篇筆者儘快寫出來,只是一些 關於 ListPopupWindow 和 PopupWindow 的具體用法,相關源碼會上傳 github ;
不介意各位讀者,幫忙點個心或者是加個關注哈 (︶.̮︶✽) !

做者:吾乃韓小呆 連接:https://www.jianshu.com/p/3a8dd7a4b41a 來源:簡書 簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。