【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导航条组件时,如果你的导航条带有下拉 ...
随机推荐
- [转]C#中图片.BYTE[]和base64string的转换
本文转自:http://blog.csdn.net/thebesttome/article/details/6870155 在C#中 图片到byte[]再到base64string的转换: Bitma ...
- 学习mfc书籍
Visual C++ and MFC Programming http://www.math.hcmuns.edu.vn/~tatuana/C%20For%20Win/MFC/Tai%20Lieu%2 ...
- TortoiseSVN解决冲突
解决冲突Resolving Conflicts有时候,你从仓库更新文件时会发生一些冲突.当两个或更多开发人员对同一个文件的某几行做 了修改,就会产生冲突.因为Subversion对你的项目一无所知,他 ...
- What is the fastest way of (not) logging?
原文地址:http://www.slf4j.org/faq.html#logging_performance SLF4J supports an advanced feature called par ...
- thymleaf th:if标签
1.概念 <table> <tr> <th>NAME</th> <th>PRICE</th> <th>IN STOC ...
- go语言基础之break和continue的区别
1.break和continue的区别 在循环里面有两个关键操作break和continue,break操作是跳出当前循环,continue是跳过本次循环. 2.break 备注:break可⽤于fo ...
- 一个十分简洁实用的MD风格的UI主框架
2017-5-23 详见:https://github.com/baiqiantao/CheesesquareSample MainActivity public class MainActivity ...
- .NET中的六个重要概念
内容导读 概述 当你声明一个变量背后发生了什么? 堆和栈 值类型和引用类型 哪些是值类型,哪些是引用类型? 装箱和拆箱 装箱和拆箱的性能问题 一.概述 本文会阐述六个重要的概念:堆.栈.值类型.引用类 ...
- java学习笔记10--泛型总结
java学习笔记系列: java学习笔记9--内部类总结 java学习笔记8--接口总结 java学习笔记7--抽象类与抽象方法 java学习笔记6--类的继承.Object类 java学习笔记5-- ...
- MyEclipse 全面的快捷键
摘自: http://www.360doc.com/content/11/0406/10/6704374_107513559.shtml 引用 MyEclipse快捷键(全面) 程序代码自动排版:Ct ...