Android PopupWindow 实现自定义弹出层
2012-11-16 16:53
549 查看
最近做的android项目,需要实现弹出层,并灰掉弹出层后面的部分。大致需要做的效果为,点击more时,弹出一层只遮罩中间部分(由图一到图二),效果图如下(个人随便写的样式,勿怪哈),废话不说,直接上思路代码。
![](http://img.my.csdn.net/uploads/201211/19/1353338441_3452.png)
![](http://img.my.csdn.net/uploads/201211/19/1353338570_2257.png)
图一 图二
至此,主要代码完成。备注:popup_enter.xml和popup_exit.xml两个文件放在res下面的anim文件夹中,弹出层的页面没有贴代码,大家可以根据自己的需求来编写弹出页面的内容。
![](http://img.my.csdn.net/uploads/201211/19/1353338441_3452.png)
![](http://img.my.csdn.net/uploads/201211/19/1353338570_2257.png)
图一 图二
点击more,调用的代码
/** * 点击more触发的方法,弹出显示所有年级学科 * @param view */ public void showAllSubject(View view) { if (null == mPopupWindow) { View layout = LayoutInflater.from(this).inflate( R.layout.activity_allgrade, null); int width = subjectListView.getWidth(); int height = subjectListView.getHeight(); //设置弹出部分和面积大小 mPopupWindow = new PopupWindow(layout, width, height, true); //设置动画弹出效果 mPopupWindow.setAnimationStyle(R.style.PopupAnimation); // 设置半透明灰色 ColorDrawable dw = new ColorDrawable(0x7DC0C0C0); mPopupWindow.setBackgroundDrawable(dw); mPopupWindow.setClippingEnabled(true); } int[] pos = new int[2]; subjectListView.getLocationOnScreen(pos); mPopupWindow.showAtLocation(subjectListView, Gravity.RIGHT | Gravity.TOP, pos[0], pos[1]); }
弹出效果配置
<style name="PopupAnimation" parent="android:Animation"> <item name="android:windowEnterAnimation">@anim/popup_enter</item> <item name="android:windowExitAnimation">@anim/popup_exit</item> </style>
popup_enter.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <scale android:duration="100" android:fromXScale="1.0" android:fromYScale="0" android:pivotX="50%" android:pivotY="0%" android:toXScale="1.0" android:toYScale="1.0" /> </set>
popup_exit.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <scale android:duration="50" android:fromXScale="1.0" android:fromYScale="1.0" android:pivotX="50%" android:pivotY="0%" android:toXScale="1.0" android:toYScale="0" /> </set>
至此,主要代码完成。备注:popup_enter.xml和popup_exit.xml两个文件放在res下面的anim文件夹中,弹出层的页面没有贴代码,大家可以根据自己的需求来编写弹出页面的内容。
相关文章推荐
- Android自定义popupwindow实现ios中UC浏览器中长按图片弹出的PopupWindow效果
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- android自定义spinner,使用AppCompatTextView+PopupWindow 实现下拉选择的功能
- PopupWindow进阶用法——android上实现类似UCweb的自定义menu,完全模拟系统事件
- 【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单
- Android popupWindow弹出窗体实现方法分析
- Android PopupWindow实现从顶部弹出下拉菜单左、中、右
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- Android-PopupWindow自定义的弹出框
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- android PopupWindow 和 Activity弹出窗口实现方式
- Android手机UI设计---”知乎“界面外观模仿篇(二)---在Fragment里使用自定义listview以及popupwindow弹出窗口
- Android PopupWindow的使用技巧 Android上自定义弹出窗口
- 【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单
- Android PopupWindow实现右侧、左侧和底部弹出菜单
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- Android自定义View之popupwindow进阶封装:高仿ios “item动画弹出”效果的popupwindow。
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- Android自定义弹出窗口PopupWindow使用技巧
- android自定义弹出框样式实现