Android PopupWindow详解
来源:互联网 发布:萨斯喀彻温大学知乎 编辑:程序博客网 时间:2024/06/17 20:03
构造方法:
1、public PopupWindow(View contentView, int width, int height, boolean focusable)
contentView为要显示的view,width和height为宽和高,值为像素值,也可以是MATCHT_PARENT和WRAP_CONTENT。
2、其他几种比较常用的构造方法
public PopupWindow (Context context)public PopupWindow(View contentView, int width, int height)public PopupWindow(View contentView)
其中第一种最省事,构造函数中设置了要显示的View,宽度 ,高度以及是否能获得焦点。
改变PopupWindow的视图内容
public void setContentView(View contentView)方法PopupWindow popupWindow = new PopupWindow(context);popupWindow.setContentView(contentview);
获得PopupWindow的视图内容
public View getContentView()方法
显示PopupWindow:
showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
设置大小:
有两种方法设置PopupWindow的大小:
1、调用有宽高参数的构造函数:
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);View contentview = inflater.inflate(R.layout.popup_process, null);PopupWindow popupWindow = new PopupWindow(contentview,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
2、通过setWidth和setHeight设置
PopupWindow popupWindow = new PopupWindow(contentview);popupWindow.setWidth(LayoutParams.WRAP_CONTENT); popupWindow.setHeight(LayoutParams.WRAP_CONTENT)
这里的WRAP_CONTENT可以换成fill_parent 也可以是具体的数值,它是指PopupWindow的大小,也就是contentview的大小,注意popupwindow根据这个大小显示你的View,如果你的View本身是从xml得到的,那么xml的第一层view的大小属性将被忽略。相当于popupWindow的width和height属性直接和第一层View相对应。
popupWindow 设置为WRAP_CONTENT ,我想得到的是一个宽150dip 高80dip的popupwindow,需要额外加一层
LinearLayout ,这个LinearLayout 的layout_width和layout_height为任意值。而我们真正想显示的View 放在第二层,并且 android:layout_width=”150.0dip” android:layout_height=”80.0dip”
如:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="119dp" android:layout_height="90dp" android:layout_centerHorizontal="true" android:layout_gravity="end" android:background="#e0e0e0"> <TextView android:id="@+id/all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="19dp" android:text="全部" /> <TextView android:id="@+id/mine" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="15dp" android:text="已关注" /> </RelativeLayout></RelativeLayout>
PopUpWindow的焦点:
setFocusable(true);
则PopUpWindow本身可以看作一个类似于模态对话框的东西(但有区别),PopupWindow弹出后,所有的触屏和物理按键都有PopupWindows处理。其他任何事件的响应都必须发生在PopupWindow消失之后, (home 等系统层面的事件除外)。比如这样一个PopupWindow出现的时候,按back键首先是让PopupWindow消失,第二次按才是退出activity,准确的说是想退出activity你得首先让PopupWindow消失,因为不并是任何情况下按back PopupWindow都会消失,必须在PopupWindow设置了背景的情况下 。
如果PopupWindow中有Editor的话,focusable要为true。
而setFocusable(false);
则PopUpWindow只是一个浮现在当前界面上的view而已,不影响当前界面的任何操作。
是一个“没有存在感”的东西。
一般情况下setFocusable(true);
点击空白处的时候让PopupWindow消失
要让点击PopupWindow之外的地方PopupWindow消失你需要调用setBackgroundDrawable(new BitmapDrawable());
设置背景,为了不影响样式,这个背景是空的。还可以这样写,觉得这样要保险些:
setBackgroundDrawable(new ColorDrawable(0x00000000));
PopupWindow设置动画
很多时候我们把PopupWindow用作自定义的菜单,需要一个从底部向上弹出的效果,这就需要为PopupWindow添加动画。
设置动画的方法:
public void setAnimationStyle(int animationStyle)
在res/value/styles.xml添加一个style
<style name="anim_menu_bottom"> <item name="android:windowEnterAnimation">@anim/bar_in</item> <item name="android:windowExitAnimation">@anim/bar_out</item></style>
在工程res下新建anim文件夹,在anim文件夹先新建两个xml文件
bar_in.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:pivotX="50%" android:pivotY="50%" android:duration="250" android:fromXScale="0" android:toXScale="1" android:fromYScale="0" android:toYScale="1"/></set>
bar_out.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:pivotX="50%" android:pivotY="50%" android:duration="250" android:fromXScale="1" android:toXScale="0" android:fromYScale="1" android:toYScale="0"/></set>
mPopupWindow.setAnimationStyle(R.style.menu_anim_bottombar);
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android PopupWindow详解
- Android PopupWindow详解
- Android PopupWindow详解
- Android PopupWindow使用详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- Android PopupWindow详解
- Android进阶:PopupWindow详解
- Android中的PopupWindow详解
- Android中的PopupWindow详解
- android PopupWindow详解
- httpd
- 百度通用搜索代码
- 前端优化:九个技巧,提高Web性能
- iOS viewController添加导航条添加跳转以及特效
- PAT BASIC 1013 数素数
- Android PopupWindow详解
- Android 6.0 - 动态权限管理的解决方案
- centos 上安装 postgresql 9.5
- JDK源码之解读String最终类的equals()和hashcode()方法实现原理
- kudu vs parquet, impala vs spark Benchmark
- AlphaBeta剪枝算法
- 解决node-sass安装失败的问题
- 教你读懂网络请求的瀑布图
- 一树花的记忆