android 下拉窗口菜单的实现(popupwindow)
来源:互联网 发布:网络扑克 编辑:程序博客网 时间:2024/05/22 03:51
这几天在写一个记事本的简单app,碰到很多菜单项,故专门抽出时间研究一下下拉式菜单的实现。首先说明一下,本人新手,刚学android没多久,此文章只用来记录我的成长历程以及方便他人借鉴一下。欢迎大家指出不足和改进之处!
今天花了一下午专门查找整理和总结实现方法,感觉popupwindow的实现方式比较简单,故采用此方法来实现。先来简单说一下思路:popupwindow,是android对话框的一种,与Alertdialog类似,不过popupwindow的位置使用更加方便灵活。所以,只要为popupwindow添加动画效果就可以实现想要的功能啦。
具体关于popupwindow的详细介绍可以看大神的文章:http://blog.csdn.net/harvic880925/article/details/49272285
这里我就直接上与其相关的代码了:
public PopupWindow(View contentView, int width, int height);//构造方法
popupwindow构造方法需要接收一个窗口的自定义布局,如果是想做成菜单的话大家可以在里面随意布局,自由度挺高的。
首先是布局文件:
这几天在写一个记事本的简单app,碰到很多菜单项,故专门抽出时间研究一下下拉式菜单的实现。首先说明一下,本人新手,刚学android没多久,此文章只用来记录我的成长历程以及方便他人借鉴一下。欢迎大家指出不足和改进之处!今天花了一下午专门查找整理和总结实现方法,感觉popupwindow的实现方式比较简单,故采用此方法来实现。先来简单说一下思路:popupwindow,是android对话框的一种,与Alertdialog类似,不过popupwindow的位置使用更加方便灵活。所以,只要为popupwindow添加动画效果就可以实现想要的功能啦。具体关于popupwindow的详细介绍可以看大神的文章:http://blog.csdn.net/harvic880925/article/details/49272285 这里我就直接上与其相关的代码了:public PopupWindow(View contentView, int width, int height);//构造方法popupwindow构造方法需要接收一个窗口的自定义布局首先是布局文件:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/note_detail" android:background="#3366ee" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="批量删除" /> <Button android:id="@+id/note_update" android:background="#3366ee" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="全部删除" /> <Button android:id="@+id/note_delete" android:background="#3366ee" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="其他功能" /></LinearLayout>接着写下拉窗口的实现方法:
public void showPopupWindow(View parent){//显示下拉弹出的窗口WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); // 显示的宽度为:屏幕的宽度的一半 int xpos=windowManager.getDefaultDisplay().getWidth()/2;if(mPop==null){//注意,布局文件的控件设置为fill_parent,否则此处初始化的窗口大小不会变化mPop = new PopupWindow(getLayoutInflater().inflate(R.layout.popup_window, null),xpos, LayoutParams.WRAP_CONTENT);//实例化一个popupwindow对象}// 聚焦 mPop.setFocusable(true); // 设置允许在外点击消失 mPop.setOutsideTouchable(true); //这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景 mPop.setBackgroundDrawable(new BitmapDrawable()); mPop.setAnimationStyle(R.style.popupwindow_animation_style);//设置动画 mPop.showAsDropDown(parent, 0, 0); }}注意:popupwindow的布局文件中控件宽度设置成fill_parent,而不是wrap_content,否则在后面的方法中改变其大小时你会发现窗口并没有变化!这个错误我找了好久的资料才发现= =,因为他们基本没提,可能是太小儿科了,希望大家别像我一样。
下面的动画实现就比较容易了,直接贴代码了。
首先新建一个出现动画的xml:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:interpolator="@android:anim/linear_interpolator" android:fromYDelta="-100%" android:toYDelta="0%" android:duration="500"/></set>再建消失动画xml:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromYDelta="0%" android:toYDelta="-100%" android:duration="500"/></set>在style文件中描述:
<style name="popupwindow_animation_style"> <item name="android:windowEnterAnimation">@anim/drawer_in</item> <item name="android:windowExitAnimation">@anim/drawer_out</item> </style>然后在
public void showPopupWindow(View parent)中调用
setAnimationStyle()方法就ok了。
到这里,下拉菜单窗口就实现了。大家有如果有意见和建议,欢迎指出!谢谢
0 0
- android 下拉窗口菜单的实现(popupwindow)
- 【Android】通过PopupWindow实现浏览器风格的下拉菜单
- Popupwindow实现下拉菜单
- android PopupWindow 下拉菜单
- Popupwindow实现自定义的下拉菜单
- 下拉菜单(二) PopupWindow 封装实现
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)!!!!
- Android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- Android弹出窗口的实现(PopupWindow)
- 用PopupWindow实现下拉菜单
- linux之 DeviceTree基础
- 文件的创建删除读写操作
- cookie应用之显示当前时间,上次时间
- hashMap,treeMap,LinkedHashMap使用以及区别分析
- 货币、网络与未来——区块链的起源、发展及影响
- android 下拉窗口菜单的实现(popupwindow)
- ViewPager+Fragment练习first
- PopupWindow popupWindow.showAtLocation(view,Gravity.TOP,x,y) 被顶部系统状态栏挡住内容
- Matlab与线性代数 -- 向量的范数
- android stuio修改代码参考线和智能换行
- map set list三种集合之间的关系及各自的特点
- 【五年】Java打怪升级之路
- Android相关技术博客汇总
- Linux下tar命令详解