筆者數了數枕巾脫落的頭發(fā),發(fā)現了 Android 開發(fā)常用的類似于彈窗的懸浮窗口 大概是 PopupWindow 和 dialog。之前有還算具體的寫過關于 dialog 以及自定義 dialog 甚至是 dialogActivity 的相關筆記甩鳄,即:Android 系統(tǒng)原生dialog使用 、Android dialog Activity 使用 掩宜、android自定義相對復雜dialog 戚扳。突然感覺 PopupWindow 這個說簡單不簡單,說難不難的知識點也應該詳細研究一下魁巩。
下面回歸正題急灭,請欣賞筆者的表演:
一、創(chuàng)建 PopupWindow
先說一下 Google 爸爸提供的構造方法
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_CONTENT
和ViewGroup.LayoutParams.MATCH_PARENT
也可以是具體的數值了畴嘶。但是 這里規(guī)定的 是 PopupWindow 內加載到的 view 對象的大小蛋逾。相當于 PopupWindow 設置的值 為 xml 內最外層布局是等效的。
如果窗悯,需要控制加載展示的 view 大小区匣,那么需要將該參數設置ViewGroup.LayoutParams.WRAP_CONTENT
并且 目標樣式外再包裹一層布局,類似于:
<?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 focusable
PopupWindow 響應內部的點擊事件蒋院,例如 TextView亏钩、Button 等
Context context
上下文,不多講
剩下下面的三個構造方法欺旧,不常用姑丑,但是會自定義 view 的大佬都知道啥意思。
二切端、常用設置方法
//設置動畫特效 即 展示和消失動畫
public void setAnimationStyle(int animationStyle)
//主要作用是為了設置 PopupWindow 顯示的時候是否會與 StatusBar 重疊(如果存在的話也包括 SystemBar )
public void setAttachedInDecor(boolean enabled)
//設置 PopupWindow 的背景彻坛。該屬性不設置的會,會導致 PopupWindow 出現后不會消失踏枣,即便是 點擊 back 鍵也不起作用昌屉。這應該是 PopupWindow 較為變態(tài)的地方。
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)
//監(jiān)聽 PopupWindow關閉的事件
public void setOnDismissListener(OnDismissListener onDismissListener)
//設置 點擊 PopupWindow意外區(qū)域竞帽,隱藏 popupWindow 然而并沒有什么卵用
public void setOutsideTouchable(boolean touchable)
// PopupWindow觸摸時的監(jiān)聽回調
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()
三谍肤、常用獲取方法
//獲取 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)
四荒揣、常用判斷方法
//判斷 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()
五、不常使用 更新位置或更新內容方法
//更新當前 PopupWindow
public void update()
//更新 Popwindow 到新的形態(tài) 參數:長度焊刹、寬度
public void update(int width, int height)
//更新 Popwindow 到新的位置系任、新的形態(tài) 參數:新位置的坐標寒匙,長度豆村、寬度
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)
六、部分主要事項
1、
PopupWindow 默認是不允許超出窗口的河绽,具體演示請看下篇筆記损话;
2虑凛、
PopupWindow 的 public void setOutsideTouchable(boolean touchable)
方法默認沒有什么卵用 腌且,是否可以點擊 PopupWindow 外部進行隱藏 PopupWindow ,必須設置 public void setBackgroundDrawable(Drawable background)
該方法
3颁糟、
getHeight()
和 getWidth()
方法不太好用航背,一般返回為 0;如果獲取 PopupWindow 的長寬建議使用如下方法:
devPop.getContentView().measure(0, 0);
int hight=devPop.getContentView().getMeasuredHeight();
int width=devPop.getContentView().getMeasuredWidth();
4棱貌、
將 PopupWindow 的四角設置為圓角方法玖媚,可以將事先準備的 view 的父布局加一個帶有圓角的 shape background, 之后設置 PopupWindow 的 setBackgroundDrawable() 方法 設置為devPop.setBackgroundDrawable(new ColorDrawable(0x00ffffff));
5婚脱、
一定要在顯示 PopupWindow 的界面的 OnDestroy()方法內調用 dismiss()方法今魔,否則會產生 “意想不到”的驚喜。
@Override
protected void onDestroy() {
super.onDestroy();
if (popupWindow != null) {
popupWindow.dismiss();
}
}
6障贸、
筆者遇到的注意事項错森,感覺目前就這么多了,之后如果遇到還會補充篮洁;
七涩维、附贈 ListPopupWindow 相關漢語 API
1、效果展示
或許各位很多人都知道 PopupWindow 但是 少有人知道 ListPopupWindow 袁波,故 展示效果如下瓦阐,具體應用 請留意之后的 筆記。
2篷牌、相關 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 設置為模態(tài)框
public void setModal(boolean modal)
//設置 ListPopupWindow 提示位置
public void setPromptView( View prompt)
//設置 ListPopupWindow 的選定位置
public void setSelection(int position)
//設置 ListPopupWindow 輸入區(qū)域的輸入模式
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)、監(jiān)聽器
//設置 ListPopupWindow 關閉的 時的監(jiān)聽
public void setOnDismissListener(PopupWindow.OnDismissListener listener)
//設置 ListPopupWindow 的 item 單擊時的監(jiān)聽回調
public void setOnItemClickListener(AdapterView.OnItemClickListener clickListener)
//設置 ListPopupWindow item 被選擇 時的監(jiān)聽回調
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)听诸、判斷方法
//判斷是否為 模態(tài)框樣式
public boolean isModal()
//判斷是否為顯示狀態(tài)
public boolean isShowing()
//過了關鍵按鍵 點擊事件
public boolean onKeyDown(int keyCode, KeyEvent event)
//過濾關鍵按鍵 點擊抬起事件
public boolean onKeyUp(int keyCode, KeyEvent event)
//判斷 是否 在指定 item 下執(zhí)行點擊事件
public boolean performItemClick(int position)
(6)、其他的重要方法
//執(zhí)行相關 對 ui 線程的操作
public void postShow()
//刪除 當前列表的所有選擇
public void clearListSelection()
//關閉 ListPopupWindow
public void dismiss()
3蚕泽、關于 ListPopupWindow 的其它 雜談
當發(fā)現這個 ListPopupWindow 算是控件的工具值晌梨,筆者舍去很多地方的 ListView 和部分AutoCompleteTextView 和 Spinner 控件使用桥嗤,因為 ListPopupWindow 搭配 TextView 或者搭配 EditText 極其好用。
今天學習就到這里仔蝌,筆者希望可以幫助各位在伙伴泛领,歡迎大佬指點、批評還有建議敛惊;
之后的幾篇筆者盡快寫出來渊鞋,只是一些 關于 ListPopupWindow 和 PopupWindow 的具體用法,相關源碼會上傳 github 瞧挤;
不介意各位讀者锡宋,幫忙點個心或者是加個關注哈 (︶.?︶?) !