PopupWindow的簡單使用示例
public class
PopupWindow
extends Object
java.lang.Object
????
android.widget.PopupWindow
Class Overview
A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.
PopupWindow的構(gòu)造函數(shù):
//方法一:?? public?PopupWindow?(Context?context)?? //方法二:?? public?PopupWindow(View?contentView)?? //方法三:?? public?PopupWindow(View?contentView,?int?width,?int?height)?? //方法四:?? public?PopupWindow(View?contentView,?int?width,?int?height,?boolean?focusable)
注意:這里有四個構(gòu)造函數(shù),但要生成一個PopupWindow最基本的三個條件是一定要設(shè)置的:View contentView,int width, int height ;少任意一個就不可能彈出來PopupWindow!!
構(gòu)造一個PopupWindow:
LayoutInflater?inflater?=?(LayoutInflater)?getSystemService(Context.LAYOUT_INFLATER_SERVICE); View?view?=?inflater.inflate(R.layout.popwindowlayout,?null); //?下面是兩種方法得到寬度和高度?getWindow().getDecorView().getWidth() PopupWindow?window?=?new?PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
PopupWindow沒有默認布局,所以需要設(shè)置width和height。 設(shè)置顯示的位置的方法:
//相對某個控件的位置(正左下方),無偏移?? showAsDropDown(View?anchor):?? //相對某個控件的位置,有偏移;xoff表示x軸的偏移,正值表示向左,負值表示向右;yoff表示相對y軸的偏移,正值是向下,負值是向上;?? showAsDropDown(View?anchor,?int?xoff,?int?yoff):?? //相對于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設(shè)置偏移或無偏移?? showAtLocation(View?parent,?int?gravity,?int?x,?int?y):
設(shè)置其他屬性的函數(shù):
public?void?dismiss()?? public?void?setFocusable(boolean?focusable)??//設(shè)置窗體可點擊 public?void?setTouchable(boolean?touchable)?? public?void?setOutsideTouchable(boolean?touchable)?? public?void?setBackgroundDrawable(Drawable?background)??//設(shè)置半透明,透明等背景
簡單示例:(setAnimationStyle()、showAtLocation()) 布局文件activity_main.xml
PopupWindow彈出窗口的布局popwindowlayout.xml
窗口隱藏的動畫:res/anim/pophidden_anim.xml
窗口顯示的動畫:res/anim/popshow_anim.xml
在res/values/styles.xml 中添加動畫類型:
@anim/popshow_anim@anim/pophidden_anim"?_ue_custom_node_="true">
點擊按鈕彈出駕校介紹窗口,點擊屏幕別處,窗口消失。