关于PopupWindow的使用例子
2011-08-12 14:32
417 查看
先上图
用的豌豆荚截图,本来是个动画的,每个过程都有几张,大概就是要实现这个效果,初始状态只有一个Button,当点击show的时候,另外一个页面从底部慢慢升起来,直到覆盖到上一个页面,注意这里不是启用另一个Activity,是用的PopupWindow,当点击dismiss的时候,又慢慢消失。。。这种效果看上去不错,PopupWindow上面可以添加想要添加的控件,比如ListView(注意,如果添加ListView的话,当它弹出来之后,按back键不起作用,它获取不了监听,其余的非ListView控件可以,这里添加了个Button ),下面贴出代码
这是主类MainActivity.java
布局文件main.xml
布局文件popup.xml
工程结构
注意看在res文件夹下面新建了一个anim文件夹,里面要实现的动画页面,比如从哪个坐标移动到哪个坐标之类的,当然还可以定义其它的,这里只实现了Y坐标的移动
在anim文件夹下面建两个文件,一个是in.xml,另外一个是out.xml,意思一看就明白
in.xml
它表示Y的坐标从854(因为我的手机分辨率是850x480,摩托MB525)移动到0,时间为1秒
out.xml
这个不解释了。。。
有人给我反应说缺少style文件,我看了一下,确实是,当时发贴的时候漏掉了,现在补上
在values文件夹下面建一个styles.xml文件
现在应该OK了
用的豌豆荚截图,本来是个动画的,每个过程都有几张,大概就是要实现这个效果,初始状态只有一个Button,当点击show的时候,另外一个页面从底部慢慢升起来,直到覆盖到上一个页面,注意这里不是启用另一个Activity,是用的PopupWindow,当点击dismiss的时候,又慢慢消失。。。这种效果看上去不错,PopupWindow上面可以添加想要添加的控件,比如ListView(注意,如果添加ListView的话,当它弹出来之后,按back键不起作用,它获取不了监听,其余的非ListView控件可以,这里添加了个Button ),下面贴出代码
这是主类MainActivity.java
package com.test.popupwindow; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout.LayoutParams; import android.widget.PopupWindow; publicclass MainActivity extends Activity { /** Called when the activity is first created. */ boolean flag =false; PopupWindow popupWindow; @Override publicvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } publicvoid init() { Button btn_show = (Button) findViewById(R.id.btn_show); LayoutInflater inflater = LayoutInflater.from(this); View layout = inflater.inflate(R.layout.popup, null); popupWindow =new PopupWindow(layout, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); Button btn_dismiss = (Button) layout.findViewById(R.id.btn_dismiss); btn_dismiss.setOnClickListener(new OnClickListener() { @Override publicvoid onClick(View v) { // TODO Auto-generated method stub openMenu(); } }); btn_show.setOnClickListener(new OnClickListener() { @Override publicvoid onClick(View v) { // TODO Auto-generated method stub openMenu(); } }); } publicvoid btn_showOnClicked() { openMenu(); } publicvoid btn_dismissOnClicked() { openMenu(); } publicvoid openMenu() { if (!flag) { popupWindow.setAnimationStyle(R.style.PopupAnimation); popupWindow.showAtLocation(findViewById(R.id.btn_show), Gravity.NO_GRAVITY, 0, 0); popupWindow.setFocusable(true); popupWindow.update(); flag =true; } else { popupWindow.dismiss(); popupWindow.setFocusable(false); flag =false; } } }
布局文件main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/layout" > <Button android:id="@+id/btn_show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="show" /> </RelativeLayout>
布局文件popup.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#cccccc" > <Button android:id="@+id/btn_dismiss" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="dismiss"/> </LinearLayout>
工程结构
注意看在res文件夹下面新建了一个anim文件夹,里面要实现的动画页面,比如从哪个坐标移动到哪个坐标之类的,当然还可以定义其它的,这里只实现了Y坐标的移动
在anim文件夹下面建两个文件,一个是in.xml,另外一个是out.xml,意思一看就明白
in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator"> <translate android:fromYDelta="854" android:toYDelta="0" android:duration="1000"/> </set>
它表示Y的坐标从854(因为我的手机分辨率是850x480,摩托MB525)移动到0,时间为1秒
out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:interpolator="@android:anim/decelerate_interpolator" android:fromYDelta="0" android:toYDelta="854" android:duration="10000" /> </set>
这个不解释了。。。
有人给我反应说缺少style文件,我看了一下,确实是,当时发贴的时候漏掉了,现在补上
在values文件夹下面建一个styles.xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="PopupAnimation" parent="android:Animation" mce_bogus="1"> <item name="android:windowEnterAnimation">@anim/in</item> <item name="android:windowExitAnimation">@anim/out</item> </style> </resources>
现在应该OK了
相关文章推荐
- 关于apply属性使用的一些例子
- Android学习小Demo(14)Android中关于PopupWindow的使用
- 关于java中Properties类的简单的使用例子
- 一个关于jqGrid使用的小例子(行按钮)
- 关于checkboxtree选中的一个小例子非常使用
- java关于ArrayList动态数组与静态数组Arrays-元素比较输出最大最小值的使用例子
- PopupWindow的使用例子
- 关于java中Properties类的简单的使用例子
- C#中关于在LinkedList集合类中使用泛型的例子
- 关于使用PopupWindow的遇到的一些小问题
- 关于ico中autofac的使用的小例子
- 这个关于ATTRIBUTE的使用方法的例子还算可以
- 2道关于委托事件的使用的例子
- 关于PopupWindow的showAsDropDown()和showAtLocation()使用方式,popupWindow的各种弹出
- 关于apply属性使用的一些例子(转载来自用户:tan_dan)
- 关于java中Properties类的简单的使用例子
- Andriod学习笔记(12)---关于Timer的使用例子
- 关于linux系统中命令的使用详解1(附带有例子说明)
- 关于使用PopupWindow
- 关于null影响索引使用的一例子