【android开发】使用PopupWindow实现页面点击顶部弹出下拉菜单
没有太多花样,也没有很复杂的技术,就是简单的PopupWindow的使用,可以实现点击弹出一个自定义的view,view里可以随便设计,常用的可以放一个listview。
demo中我只是一个点击展示,简单的使用了fade in out的动画效果,也没有精美的图片资源,看着也丑,不过这么短的时间,让你掌握一个很好用的技术,可以自己扩展,不很好么?
废话不说了,直接上代码:
MainActivity.java
- public class MainActivity extends Activity implements OnClickListener {
- private PopupWindow popupwindow;
- private Button button;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button = (Button) findViewById(R.id.button1);
- button.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.button1:
- if (popupwindow != null&&popupwindow.isShowing()) {
- popupwindow.dismiss();
- return;
- } else {
- initmPopupWindowView();
- popupwindow.showAsDropDown(v, 0, 5);
- }
- break;
- default:
- break;
- }
- }
- public void initmPopupWindowView() {
- // // 获取自定义布局文件pop.xml的视图
- View customView = getLayoutInflater().inflate(R.layout.popview_item,
- null, false);
- // 创建PopupWindow实例,200,150分别是宽度和高度
- popupwindow = new PopupWindow(customView, 250, 280);
- // 设置动画效果 [R.style.AnimationFade 是自己事先定义好的]
- popupwindow.setAnimationStyle(R.style.AnimationFade);
- // 自定义view添加触摸事件
- customView.setOnTouchListener(new OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (popupwindow != null && popupwindow.isShowing()) {
- popupwindow.dismiss();
- popupwindow = null;
- }
- return false;
- }
- });
- /** 在这里可以实现自定义视图的功能 */
- Button btton2 = (Button) customView.findViewById(R.id.button2);
- Button btton3 = (Button) customView.findViewById(R.id.button3);
- Button btton4 = (Button) customView.findViewById(R.id.button4);
- btton2.setOnClickListener(this);
- btton3.setOnClickListener(this);
- btton4.setOnClickListener(this);
- }
- }
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"
- android:background="#000000"
- tools:context=".MainActivity" >
- <Button
- android:id="@+id/button1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:gravity="center"
- android:background="#C0C0C0"
- android:text="点击下拉列表" />
- </RelativeLayout>
自定义view的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"
- android:background="#C0C0C0" >
- <Button
- android:id="@+id/button2"
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:paddingRight="70dp"
- android:text="viviens" />
- <Button
- android:id="@+id/button3"
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_below="@+id/button2"
- android:paddingRight="70dp"
- android:text="mryang" />
- <Button
- android:id="@+id/button4"
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_below="@+id/button3"
- android:paddingRight="70dp"
- android:text="张晓达" />
- </RelativeLayout>
动画效果:
inputodown.xml 进入屏幕
- <?xml version="1.0" encoding="UTF-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <translate
- android:duration="500"
- android:fromYDelta="-100%"
- android:toYDelta="0" />
- </set>
outdowntoup.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <translate
- android:duration="500"
- android:fromYDelta="0"
- android:toYDelta="-100%" />
- </set>
styles.xml
- <style name="AnimationFade">
- <!-- PopupWindow左右弹出的效果 -->
- <item name="android:windowEnterAnimation">@anim/inuptodown</item>
- <item name="android:windowExitAnimation">@anim/outdowntoup</item>
- </style>
实现效果:
demo地址:
http://download.csdn.net/detail/mad1989/5518035
【android开发】使用PopupWindow实现页面点击顶部弹出下拉菜单的更多相关文章
- 有序无序ul->li ol->li菜单,默认点击当前弹出下拉,再次点击收起下拉菜单
实现这一效果利用css和js技术结合 以ul->li为例子 <!DOCTYPE html><html lang="en"><head> & ...
- 有序无序Ul->Li Ol->Li菜单,默认点击当前弹出下拉,再次点击收起下拉菜单(变形2 ---修饰)
从上面可以看出,两个问题,第一:下拉出现的太快太突然,第二:再点击下一个下拉菜单的时候,上一个不会闭合,针对这两个问题,接下来会一 一解决. 解决下拉太快: js中有个jquery效果,有一个效果是j ...
- easyui combobox点击输入框弹出下拉框
由于easyui combobox需要点击下拉箭头才能下拉,不能像select标签那样点击输入框就下拉,所以觉得不太方便,查看了一下,combobox弹出框是一个div,原本想在他的输入框的点击事件中 ...
- 【Android初级】如何实现一个有动画效果的自定义下拉菜单
我们在购物APP里面设置收货地址时,都会有让我们选择省份及城市的下拉菜单项.今天我将使用Android原生的 Spinner 控件来实现一个自定义的下拉菜单功能,并配上一个透明渐变动画效果. 要实现的 ...
- jq自定义下拉菜单,当用户点击非自身元素(下拉菜单)本身时关闭下拉菜单
jq自定义下拉菜单,当用户点击非自身元素(下拉菜单)本身时关闭下拉菜单 截图: 代码如下: //关闭用户菜单 $(document).mousedown(function(e){ var _con = ...
- android开发之 包含EditText组件 禁止自动获取焦点弹出输入法
在EditText标签的外层Layout中加入focusableInTouchMode属性 android:focusableInTouchMode="true" 即可.
- 页面点击关闭弹出提示js代码
代码效果为: <script> window.onbeforeunload = function() { return "您好!\n我是abc\n —————————————— ...
- ActionBar点击弹出下拉框操作
首先: getActionBar().setDisplayShowTitleEnabled(false); ActionBar.LayoutParams lp = new ActionBar.Layo ...
- Bootstrap学习笔记(5)--实现Bootstrap导航条可点击和鼠标悬停显示下拉菜单
实现Bootstrap导航条可点击和鼠标悬停显示下拉菜单 微笑的鱼 2014-01-03 Bootstrap 5,281 次围观 11条评论 使用Bootstrap导航条组件时,如果你的导航条带有下拉 ...
随机推荐
- 吐血推荐250部必看电影下载 IMDB TOP 250 download
中文名: IMDB Top 250合辑 TLF-MiniSD收藏版英文名: IMDB Top 250 TLF-MiniSD Collection版本: (更新至TOP119)[MiniSD]发行日期: ...
- jq设置样式
单个样式: $(this).css("color","red"); 多个样式: $(this).css({color:"red",backg ...
- 使用openURL实现程序间带参数跳转详解
使用openURL实现程序间带参数跳转详解 实现的效果:有两款应用A与B,A打开B A --> B 1. 新建工程B,bundle ID为com.YouXianMing.B 建立一个URL 这么 ...
- linux 编译win32程序
apt-get install mingw32 int main(int argc, char *argv) { printf("Windows Compiler Test\n") ...
- lodop同一页面一次性打印多次
怎么让打印机打印双份?lodop有设置吗?SET_PRINT_COPIES名称:设置打印份数格式:SET_PRINT_COPIES(intCopies)功能:设置本次打印的份数结果:返回逻辑结果,成功 ...
- [Android Pro] 分析 Package manager has died
reference to : http://blog.csdn.net/xxooyc/article/details/50162523 这是今天遇到的一个issue,由于Binder造成的.虽然比较简 ...
- eclipse 中添加自定义 classpath 的方法,以及 javac 和 java 的一些使用细节
目标: 将 ~/java 加入classpath eclipse: 右键选中 Project, Properties, Java Build Path, Libraries, Add External ...
- BZOJ 2179 FFT快速傅立叶 题解
bzoj 2179 Description 给出两个n位10进制整数x和y,你需要计算x*y. [题目分析] 高精裸题.练手. [代码] 1.手动高精 #include<cstdio> # ...
- JNI/NDK开发指南(十)——JNI局部引用、全局引用和弱全局引用
转自:http://blog.csdn.net/xyang81/article/details/44657385 这篇文章比较偏理论,详细介绍了在编写本地代码时三种引用的使用场景和注意事项.可能看 ...
- GOOGLE突破图书馆入口IP限制之技巧
很多图书馆都要注册或要本地IP才能浏览或下载图书,有没有另类的快速办法能快速突破图书馆的入口,能随意地下载图书呢? 希望大家在GOOGLE里试试这个:“index of/ ” inurl:lib 你将 ...