【转】android创建Popwindow弹出菜单的两种方式
方法一的Activity
- package com.app.test02;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.PopupWindow;
- import android.widget.Toast;
- public class PopwindowLeft extends Activity {
- // 声明PopupWindow对象的引用
- private PopupWindow popupWindow;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_popupwindow_main);
- // 点击按钮弹出菜单
- Button pop = (Button) findViewById(R.id.popBtn);
- pop.setOnClickListener(popClick);
- }
- // 点击弹出左侧菜单的显示方式
- OnClickListener popClick = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- getPopupWindow();
- // 这里是位置显示方式,在屏幕的左侧
- popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0);
- }
- };
- /**
- * 创建PopupWindow
- */
- protected void initPopuptWindow() {
- // TODO Auto-generated method stub
- // 获取自定义布局文件activity_popupwindow_left.xml的视图
- View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,
- false);
- // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
- popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true);
- // 设置动画效果
- popupWindow.setAnimationStyle(R.style.AnimationFade);
- // 点击其他地方消失
- popupWindow_view.setOnTouchListener(new OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- // TODO Auto-generated method stub
- if (popupWindow != null && popupWindow.isShowing()) {
- popupWindow.dismiss();
- popupWindow = null;
- }
- return false;
- }
- });
- }
- /***
- * 获取PopupWindow实例
- */
- private void getPopupWindow() {
- if (null != popupWindow) {
- popupWindow.dismiss();
- return;
- } else {
- initPopuptWindow();
- }
- }
- }
方法二的Activity
- package com.app.test02;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.PopupWindow;
- public class PopwindowLeftNew extends Activity{
- private PopupWindow popupWindow;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_popupwindow_main);
- findViewById(R.id.popBtn).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- // 获取自定义布局文件activity_popupwindow_left.xml的视图
- View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,false);
- // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
- popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true);
- // 设置动画效果
- popupWindow.setAnimationStyle(R.style.AnimationFade);
- // 这里是位置显示方式,在屏幕的左侧
- popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0);
- // 点击其他地方消失
- popupWindow_view.setOnTouchListener(new OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- // TODO Auto-generated method stub
- if (popupWindow != null && popupWindow.isShowing()) {
- popupWindow.dismiss();
- popupWindow = null;
- }
- return false;
- }
- });
- }
- });
- }
- }
效果图
附:一些相关的布局文件
PopupWindow弹出菜单
- <?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="#fff" >
- <Button android:id="@+id/popBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="弹出左侧菜单" />
- </LinearLayout>
activity_popupwindow_left.xml
- <?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:background="@android:color/darker_gray"
- android:orientation="vertical"
- android:gravity="center"
- android:paddingTop="50dp">
- <Button
- android:id="@+id/open"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="打开" />
- <Button
- android:id="@+id/save"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="保存" />
- <Button
- android:id="@+id/close"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="关闭" />
- <Button
- android:id="@+id/open"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="打开" />
- <Button
- android:id="@+id/save"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="保存" />
- <Button
- android:id="@+id/close"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="关闭" />
- <Button
- android:id="@+id/open"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="打开" />
- <Button
- android:id="@+id/save"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="保存" />
- <Button
- android:id="@+id/close"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="关闭" />
- </LinearLayout>
弹出动画XML
弹出动画
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <!-- 定义从左向右进入的动画 -->
- <translate
- android:duration="500"
- android:fromXDelta="-100%"
- android:toXDelta="0" />
- </set>
弹回动画
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <!-- 定义从右向左动画退出动画 -->
- <translate
- android:duration="500"
- android:fromXDelta="0"
- android:toXDelta="-100%" />
- </set>
动画管理
- <style name="AnimationFade">
- <!-- PopupWindow左右弹出的效果 -->
- <item name="android:windowEnterAnimation">@anim/in_lefttoright</item>
- <item name="android:windowExitAnimation">@anim/out_righttoleft</item>
- </style>
【转】android创建Popwindow弹出菜单的两种方式的更多相关文章
- 【Android】创建Popwindow弹出菜单的两种方式
方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...
- JS弹出对话框的三种方式
JS弹出对话框的三种方式 我们用到了alert()方法.prompt()方法.prompt()方法,都是在网页有一个弹出框,那么就让我们探究一下他们之间的区别: 一.第一种:alert()方法 < ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)
接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...
- 怎样在Android开发中FPS游戏实现的两种方式比较
怎样在Android开发中FPS游戏实现的两种方式比较 如何用Android平台开发FPS游戏,其实现过程有哪些方法,这些方法又有哪些不同的地方呢?首先让我们先了解下什么是FPS 英文名:FPS (F ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)
Android ViewPager 中加载 Fragmenet的两种方式 一.当fragment里面的内容较少时,直接 使用fragment xml布局文件填充 文件总数 布局文件:view_one. ...
- Android中H5和Native交互的两种方式
Android中H5和Native交互的两种方式:http://www.jianshu.com/p/bcb5d8582d92 注意事项: 1.android给h5页面注入一个对象(WZApp),这个对 ...
- android 长按弹出菜单,复制,粘贴,全选
<!-- 定义基础布局LinearLayout --> <LinearLayout xmlns:android="http://schemas.android.com/ap ...
- js弹出对话框的三种方式(转)
原文地址:https://www.jb51.net/article/81376.htm javascript的三种对话框是通过调用window对象的三个方法alert(),confirm()和prom ...
- Android更改桌面应用程序launcher的两种方式
http://blog.csdn.net/mdx20072419/article/details/9632779/ launcher,也就是android的桌面应用程序.下图是我正在使用的魅族手机的l ...
随机推荐
- 更多RANK37
By LTL 经过对BDG长期的观察得出的这套理论,希望能对大家学习OI有所帮助. 入门篇 首先在掌握一门语言时(P,C都无所谓的),假设不习惯指针能够直接无视.语言之中对于入门选手来说最重要的递归. ...
- c++ primer plus(文章6版本)中国版 编程练习答案第八章
编程练习答案第八章 8.1写输出字符串的函数,存在默认参数表示输出频率,莫感觉1.(原标题太扯了,的问题的细微变化的基础上,含义) //8.1编写一个输出字符串的函数.有一个默认參数表示输出次数,默觉 ...
- poj 1160 Post Office (间隔DP)
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15966 Accepted: 8671 Desc ...
- 十天学Linux内核之第一天---内核探索工具类
原文:十天学Linux内核之第一天---内核探索工具类 寒假闲下来了,可以尽情的做自己喜欢的事情,专心待在实验室里燥起来了,因为大二的时候接触过Linux,只是关于内核方面确实是不好懂,所以十天的时间 ...
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列之开篇
原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列之开篇 前言:博客又有一段时间没有更新了,心里感觉这段时间空空的,好像什么都没有学下,所以就想写博客,所以就有了这个系列,这里当然也 ...
- Android研究之游戏开发处理按键的响应
1.onKeyDown 方法 onKeyDown 方法是KeyEvent.Callback 接口中的一个抽象方法,重写onKeyDown 方法能够监听到按键被按下的事件,我们先看看onKeyDown方 ...
- django中通过model名字获取model
django1.6, 通过字符串和get_app.get_model获得对应的object 只需要两行代码: from django.db.models import get_model get_mo ...
- CSS3+HTML5特效7 - 特殊的 Loading 效果
效果如下 实现原理: 利用CSS3的@keyframes规则创建动画效果: 使用CSS3的animation效果完成滚动切换. 代码说明: 样式表中@-webkit-keyframes及@ke ...
- Winform: use the WebBrowser to display XML with xslt, xml, xslt 转 html 字符串
原文:Winform: use the WebBrowser to display XML with xslt, xml, xslt 转 html 字符串 声明xml字符串: string xml = ...
- new 和delete
转自:http://www.cnblogs.com/charley_yang/archive/2010/12/08/1899982.html 一直对C++中的delete和delete[]的区别不甚了 ...