Android学习——PopupWindow
2015-09-18 16:12
507 查看
1.弹出 showAtLocation 2.销毁:dismiss() 3.设置退出: popupWindow_view.setOnKeyListener popupWindow_view.setOnTouchListener
1. 弹出与销毁
示例:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="popup" android:text="弹出" /> </RelativeLayout>
新建布局文件res/layout/popup.cml作为弹出窗口的布局:
<?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" android:background="#44000000"> <RatingBar android:id="@+id/ratingBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RatingBar android:id="@+id/ratingBar2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RatingBar android:id="@+id/ratingBar3" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
java代码:
public class MainActivity extends Activity { private PopupWindow pop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View contentView = View.inflate(this, R.layout.popup, null); //初始化一个PopupWindow,指定窗口大小参数。 //true 表示PopupWindow拿到焦点,目的是当点击back键时不至于整个应用程序退出 //false,点击back键时,直接退出程序 pop = new PopupWindow(contentView,100,200,true); //设置可以获得焦点,必须有焦点才能触发监听事件 contentView.setFocusable(true); contentView.setFocusableInTouchMode(true); //方式一:当按back返回键时pop退出 contentView.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { // 按back返回键,销毁PopupWindow pop.dismiss(); } return false; } }); } public void popup(View v) { //弹出PopupWindow pop.showAtLocation((View) v.getParent(), Gravity.CENTER, 0, 0); //contentView获取焦点 v.requestFocus(); } }
运行效果:
点弹出按钮后效果:
按back返回键,PopupWindow消失。
也可以给contentView添加触摸监听:
//方式二:当按到contentView的时候pop退出,不常用 contentView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { pop.dismiss(); return false; } });
注意:焦点的获取,默认的PopupWindow不响应back键退出,程序会卡死。
区别:PopupWindow与Dialog
PopupWindow:线程阻塞的,由主线程弹出,占用UI资源,窗口可以设为透明
Dialog:子线程负责弹出,线程不阻塞,但是窗口不能设为透明的。
2. AnimationStyle——设置进出场动画
(1)新建res/anim/in.xml 和res/anim/out.xml动画文件:
in.xml: <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="3000" android:fromAlpha="0" android:toAlpha="1" /> <translate android:duration="3000" android:fromXDelta="-100%" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="0" /> </set> out.xml: <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="3000" android:fromAlpha="1" android:toAlpha="0" /> <translate android:duration="3000" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="-100%" android:toYDelta="0" /> </set>
(2)新建res/values/animstyle.xml动画样式文件:
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- PopupWindow左右弹出的效果 --> <style name="AnimationFade"> <!-- PopupWindow进入效果 --> <item name="android:windowEnterAnimation">@anim/in</item> <!-- PopupWindow退出效果 --> <item name="android:windowExitAnimation">@anim/out</item> </style> </resources>
(3)在onCreate方法中为PopupWindow设置动画样式:
pop.setAnimationStyle(R.style.AnimationFade);//设置动画样式
运行效果:
进入时,从左到右,透明度从0到1
退出时,从右到左,透明度从1到0
相关文章推荐
- Android Intent打开任意文件的通用方式
- Android学习笔记:实现图片选择器功能(下)
- android学习笔记8 - xml布局简记
- SEAndroid中定义的客体类有哪些?(access_vectors)
- Android学习——上下文菜单ContextMenu
- 在Android上运行cpp-tests
- Android 百度地图开发问题----解决地图有时候加载不出来问题
- This Handler class should be static or leaks might occur 解决办法
- MQTT Paho之Android service源码分析
- Android Studio系列教程三--快捷键
- android include使用[转]
- Android: Custom View和include标签的区别
- Android SurfaceView实战 打造抽奖转盘
- android web
- android图像图像处理——自定义组件的属性
- Android 适用各种布局的loading加载框
- Android 自定义键盘 随机键盘(完全定制,向上顶布局,点击按钮进行隐藏)
- android:descendantfocusability
- Android开发进阶,让我们一起站在别人肩膀上学习
- Android批量打不同的渠道包方法-使用第三方插件