项目回顾-PopupWindow

右上菜单,可以通过 重写 onCreateOptionsMenu指定 menu, 重写 onOptionsItemSelected 来响应点击事件

不过 这个菜单在某些手机上弹出的有点卡顿,而且如果不对主题进行设置,会从actionbar 上直接弹出,而不是下面

如果想从下面弹出,要先添加一个style

    <style name="MenuStyle" parent="@style/Widget.AppCompat.Light.PopupMenu.Overflow"><item name="overlapAnchor">false</item></style>

之后在activity引用的主题中添加一行  就行了

 <item name="actionOverflowMenuStyle">@style/MenuStyle</item>

 

Menu在没有特别要求的情况还是很好用的,但是如果要求比较复杂的时候就不如用PopupWindow了

要实现如图的效果

 

 先拿到从美工拿到的图片  

 

打开sdk的 tools文件夹   打开 draw9patch.bat  把图片拖进去

画黑边   左上两个方向的黑边 表示拉伸部分

右下表示布局中控件内容显示的区域

如果画错了 按住shift 抹掉

完成之后save

 

 

布局

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3               android:layout_width="wrap_content"
  4               android:layout_height="wrap_content"
  5               android:background="@drawable/pop_bg_shadow9"
  6               android:gravity="center_horizontal"
  7               android:orientation="vertical"
  8     >
  9 
 10     <LinearLayout
 11         android:id="@+id/ll_action_config"
 12         android:layout_width="match_parent"
 13         android:layout_height="wrap_content"
 14         android:gravity="center_vertical"
 15         android:orientation="horizontal"
 16         android:padding="10dp"
 17         >
 18 
 19         <ImageView
 20             android:layout_width="25dp"
 21             android:layout_height="25dp"
 22             android:src="@drawable/pop_setting"/>
 23 
 24         <TextView
 25             android:layout_width="wrap_content"
 26             android:layout_height="wrap_content"
 27             android:paddingLeft="15dp"
 28             android:text="关联配置"
 29             android:textColor="@color/white"/>
 30 
 31     </LinearLayout>
 32 
 33     <LinearLayout
 34         android:id="@+id/ll_action_history"
 35         android:layout_width="match_parent"
 36         android:layout_height="wrap_content"
 37         android:gravity="center_vertical"
 38         android:orientation="horizontal"
 39         android:padding="10dp"
 40         >
 41 
 42         <ImageView
 43             android:layout_width="25dp"
 44             android:layout_height="25dp"
 45             android:src="@drawable/pop_history"/>
 46 
 47         <TextView
 48             android:layout_width="wrap_content"
 49             android:layout_height="wrap_content"
 50             android:paddingLeft="15dp"
 51             android:text="历史记录"
 52             android:textColor="@color/white"/>
 53 
 54     </LinearLayout>
 55 
 56     <LinearLayout
 57         android:id="@+id/ll_action_ip"
 58         android:layout_width="match_parent"
 59         android:layout_height="wrap_content"
 60         android:gravity="center_vertical"
 61         android:orientation="horizontal"
 62         android:padding="10dp"
 63         >
 64 
 65         <ImageView
 66             android:layout_width="25dp"
 67             android:layout_height="25dp"
 68             android:src="@drawable/pop_ip"/>
 69 
 70         <TextView
 71             android:layout_width="wrap_content"
 72             android:layout_height="wrap_content"
 73             android:paddingLeft="15dp"
 74             android:text="IP地址"
 75             android:textColor="@color/white"/>
 76 
 77     </LinearLayout>
 78 
 79     <LinearLayout
 80         android:id="@+id/ll_action_about"
 81         android:layout_width="match_parent"
 82         android:layout_height="wrap_content"
 83         android:gravity="center_vertical"
 84         android:orientation="horizontal"
 85         android:padding="10dp"
 86         >
 87 
 88         <ImageView
 89             android:layout_width="25dp"
 90             android:layout_height="25dp"
 91             android:src="@drawable/pop_about"/>
 92 
 93         <TextView
 94             android:layout_width="wrap_content"
 95             android:layout_height="wrap_content"
 96             android:paddingLeft="15dp"
 97             android:text="关于"
 98             android:textColor="@color/white"/>
 99 
100     </LinearLayout>
101 </LinearLayout>
View Code

 

现在要实现点击 右上菜单 弹出 这个布局,同时菜单的图标会变换。

 

在menu目录 新建一个xml    默认icon是 more_close

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><itemandroid:id="@+id/action_menu"android:icon="@drawable/more_close"android:title="菜单"app:showAsAction="always"></item>
</menu>

 

回到activity

    @Overridepublic boolean onCreateOptionsMenu(Menu menu) {this.menu = menu;getMenuInflater().inflate(R.menu.face, menu);return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case R.id.action_menu:View view=findViewById(R.id.action_menu);showActionMenuPopup(view);break;}return super.onOptionsItemSelected(item);}

 

红色部分都是关键部分

先看  showActionMenuPopup  这个弹出菜单的方法 

 

首先要改变菜单的图标

menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.more_open));

这个menu是 全局的

private Menu menu;

在 onCreateOptionsMenu 中 把这个menu初始化

 

之后 设置 PopupWindow 

 1         View view=LayoutInflater.from(this).inflate(R.layout.popup_actionmenu,null);
 2         actionmenupopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
 3         actionmenupopupWindow.setContentView(view);
 4 
 5         view.findViewById(R.id.ll_action_config).setOnClickListener(this);
 6         view.findViewById(R.id.ll_action_history).setOnClickListener(this);
 7         view.findViewById(R.id.ll_action_ip).setOnClickListener(this);
 8         view.findViewById(R.id.ll_action_about).setOnClickListener(this);
 9 
10         actionmenupopupWindow.setFocusable(true);
11         actionmenupopupWindow.setBackgroundDrawable(new BitmapDrawable());
12 
13         actionmenupopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
14             @Override
15             public void onDismiss() {
16                 menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.more_close));
17             }
18         });
19 
20         actionmenupopupWindow.showAsDropDown(v);

 

5-8  设置弹出菜单的点击事件

11  点击PopupWindow之外的地方PopupWindow消失

13-18  监听PopupWindow消失  改变菜单图标

20  showAsDropDown  从下方出现   肯定要是一个view的, 回到onOptionsItemSelected  找到右上菜单的view  传入了方法中,最后在这里使用

 

还有一个显示方法   直接指定位置,不过要测量出状态栏和actionbar的高度

actionmenupopupWindow.showAtLocation(findViewById(R.id.action_menu),Gravity.RIGHT|Gravity.TOP ,0, getStatusBarHeight(this)+getSupportActionBar().getHeight());

 

 

IOS有一个底部菜单控件 UIActionSheet

安卓方面想实现这个  有第三方的 ActionSheet    有 谷歌官方的design包  BottomSheet

用PopupWindow 也可以实现

 

rootview = LayoutInflater.from(this).inflate(R.layout.activity_main,null);

 

 1         View view= LayoutInflater.from(this).inflate(R.layout.popup_camera_sel,null);
 2         bottompopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
 3 
 4         bottompopupWindow.setContentView(view);
 5 
 6         view.findViewById(R.id.ll_gallery).setOnClickListener(this);
 7         view.findViewById(R.id.ll_camera).setOnClickListener(this);
 8         view.findViewById(R.id.ll_cancle).setOnClickListener(this);
 9 
10         WindowManager.LayoutParams lp = getWindow().getAttributes();
11         lp.alpha = 0.6f;
12         getWindow().setAttributes(lp);
13 
14         bottompopupWindow.setFocusable(true);
15         bottompopupWindow.setBackgroundDrawable(new BitmapDrawable());
16         bottompopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
17 
18         bottompopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
19             @Override
20             public void onDismiss() {
21                 WindowManager.LayoutParams lp = getWindow().getAttributes();
22                 lp.alpha = 1f;
23                 getWindow().setAttributes(lp);
24             }
25         });
26         bottompopupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);

 

10-12   18-25   改变屏幕的透明度

 

style      从底部升上来 降下去的动画 

    <style name="mypopwindow_anim_style"><item name="android:windowEnterAnimation">@anim/popshow_anim</item><!-- 指定显示的动画xml --><item name="android:windowExitAnimation">@anim/pophidden_anim</item><!-- 指定消失的动画xml --></style>

show

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="500"android:fromYDelta="50%p"android:toYDelta="0" />
</set>

hide

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:duration="500"android:fromYDelta="0"android:toYDelta="50%p" />
</set>

 

布局

<?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="wrap_content"android:background="@color/white"android:orientation="vertical"android:gravity="center"><LinearLayoutandroid:id="@+id/ll_gallery"android:layout_width="match_parent"android:layout_height="50dp"android:gravity="center"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="相册"android:textColor="#333333"android:textSize="20sp"/></LinearLayout><!--<TextView--><!--android:layout_width="200dp"--><!--android:layout_height="1dp"--><!--android:background="#838B8B"--><!--/>--><ImageViewandroid:src="@drawable/line_pop"android:scaleType="centerCrop"android:layout_width="match_parent"android:layout_height="wrap_content"/><LinearLayoutandroid:id="@+id/ll_camera"android:layout_width="match_parent"android:layout_height="50dp"android:gravity="center"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="相机"android:textColor="#333333"android:textSize="20sp"/></LinearLayout><!--<TextView--><!--android:layout_width="200dp"--><!--android:layout_height="1dp"--><!--android:background="#838B8B"--><!--/>--><ImageViewandroid:src="@drawable/line_pop"android:scaleType="centerCrop"android:layout_width="match_parent"android:layout_height="wrap_content"/><LinearLayoutandroid:id="@+id/ll_cancle"android:layout_width="match_parent"android:layout_height="50dp"android:gravity="center"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="取消"android:textSize="20sp"/></LinearLayout></LinearLayout>
View Code

 

 

 

 

最后 记得响应完点击事件之后  dismiss 关闭PopupWindow

 

转载于:https://www.cnblogs.com/demon9/p/6021980.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/456988.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android listpreference 自定义,Android ListPreference的用法一

xmlns:android"http://schemas.android.com/apk/res/android"android:key"screen_list"android:title"标题"android:summary"说明摘要">< ListPreferenceandroid:key"myListPreference"android:title"标题"…

C语言求最大公约数和最小公倍数的几种算法

求最小公倍数算法&#xff1a; 最小公倍数两整数的乘积最大公约数 求最大公约数算法&#xff1a; (1)辗转相除法 有两整数a和b&#xff1a; ① a%b得余数c ② 若c0&#xff0c;则b即为两数的最大公约数 ③ 若c≠0&#xff0c;则ab&#xff0c;bc&#xff0c;再回去执行①…

3月15日云栖精选夜读:双管齐下,MaxCompute数据上云与生态

双管齐下&#xff0c;MaxCompute数据上云与生态 作者&#xff1a;场景研读 Go语言并发机制初探 作者&#xff1a;邴越 趣拍云短视频SDK全面升级&#xff0c;简单易用引开发者点赞 作者&#xff1a;sherry是雪梨 发表在&#xff1a;趣拍云团队 阿里云机器学习平台编程模型演…

qt android glsl,基于Qt的OpenGL学习(1)—— Hello Triangle

简介要学习OpenGL的话&#xff0c;强烈安利这个教程JoeyDeVries的learnopengl&#xff0c;这里是中文翻译好的版本。教程中使用OpenGL是通过GLFW这个库&#xff0c;而在Qt中对OpenGL封装得很好&#xff0c;并且和GUI以及IO相关的处理Qt更便捷&#xff0c;学习起来更轻松。这里就…

解决:Not Found: /favicon.ico

直接说解决办法&#xff1a; &#xff08;1&#xff09;制作一个 favicon.ico图标放在<head></head>标签中 <link rel"shortcut icon" href"xxxxxxxxxx.ico" type"image/x-icon" /> <!--制作的图标&#xff0c;使用hr…

多态方法调用的解析和分派

方法调用并不等同于方法执行&#xff0c;方法调用阶段唯一的任务就是确定被调用方法的版本&#xff08;即调用哪一个方法&#xff09;&#xff0c;暂时还不涉及方法内部的具体运行过程。在程序运行时&#xff0c;进行方法调用是最普遍、最频繁的操作&#xff0c;Class文件的编译…

ES6:Set和Map

Set Set:类似数组&#xff0c;但是成员的值都是唯一的&#xff0c;没有重复。Set本身是一个构造函数&#xff0c;用来生成Set数据结构。他包含的方法&#xff1a;add: 添加某个值&#xff0c;返回Set结构本身。delete: 删除某个值&#xff0c;返回一个布尔值&#xff0c;表示是…

九九乘法表[循环嵌套]

#九九乘法表 # 1*11 # 1*22 2*24 # 1*33 2*36 3*39 # ...#循环嵌套 #行数 i 1 while i < 9:# 打印每行的内容j 1while j < i:print("%d * %d %3d " % (i, j, i * j), end)j 1print() # 换行i 1while嵌套&#xff1a;w 1 while w < 10: #外层循…

关于用VS写C程序运行时出现烫字以及乱码的问题的原因

最近在复习C语言写程序时&#xff0c;突然碰到标题上的这种情况&#xff0c;后来经过上网查找以及逐步调试才发现原来是在打印数组的时候“越界”导致的&#xff0c;因为程序在默认初始化char类型的数组时&#xff0c;初始化的值是“烫”字&#xff0c;一般情况下是字符串未初始…

javascript函数调用的各种方法!!

在JavaScript中一共有下面4种调用方式&#xff1a; (1) 基本函数调用 (2)方法调用 (3)构造器调用 (4)通过call()和apply()进行调用 1. 基本函数调用 普通函数调用模式&#xff0c;如&#xff1a; JavaScript code?1234function fn(o){…… }fn({x:1});在基本函数调用中&#x…

ARM TK1 安装kinect驱动

首先安装usb库 $ git clone https://github.com/libusb/libusb.git 编译libusb需要的工具 $ sudo apt-get install autoconf autogen $ sudo apt-get install libtool $ sudo apt-get install libudev* 编译安装 $ sudo ./autogen.sh $ sudo make $ sudo make install $ sudo l…

如何在一个html页面中提交两个post,如何在同一个页面上从Django和Ajax获得多个post请求?...

我一整天都在为这事犯愁。似乎什么都没用。这是我的情况。在我有一个Django表单&#xff0c;有两个字段&#xff1a;redirect_from&#xff0c;redirect_to。此表单有两个提交按钮&#xff1a;Validate和{}。当页面加载时&#xff0c;Submit被隐藏&#xff0c;只显示Validate。…

大数据入门:各种大数据技术的介绍

大数据我们都知道hadoop&#xff0c;可是还会各种各样的技术进入我们的视野&#xff1a;Spark&#xff0c;Storm&#xff0c;impala&#xff0c;让我们都反映不过来。为了能够更好的架构大数据项目&#xff0c;这里整理一下&#xff0c;供技术人员&#xff0c;项目经理&#xf…

高可用与负载均衡(5)之基于客户端的负载均衡

什么是客户端负载均衡 基于客户端的负载均衡&#xff0c;简单的说就是在客户端程序里面&#xff0c;自己设定一个调度算法&#xff0c;在向服务器发起请求的时候&#xff0c;先执行调度算法计算出向哪台服务器发起请求&#xff0c;然后再发起请求给服务器。 基于客户端负载均衡…

Variant 与 内存泄露

http://blog.chinaunix.net/uid-10386087-id-2959221.html 今天遇到一个内存泄露的问题。是师兄检测出来的。Variant类型在使用后要Clear否则会造成内存泄露&#xff0c;为什么呢&#xff1f; Google一下找到下面一篇文章&#xff0c;主要介绍了Com的内存泄露&#xff0c;中间有…

安装安全类软件进行了android签名漏洞修补,魅族MX3怎么升级固件体验最新比较稳定的版本...

魅族mx3固件怎么升级?flyme os系统会持续更新&#xff0c;升级魅族MX3手机系统需先下载MX3的升级固件&#xff0c;升级固件分为体验版和稳定版。魅族MX3固件有体验版和稳定版两种&#xff0c;顾名思义&#xff0c;体验版为最新版但相比稳定版来说存在更多的漏洞&#xff0c;升…

linux su切换用户提示Authentication failture的解决办法

由于ubtun系统默认是没有激活root用户的&#xff0c;需要我们手工进行操作&#xff0c;在命令行界面下&#xff0c;或者在终端中输入如下命令&#xff1a; sudo passwd Password&#xff1a;你当前的密码 Enter new UNIX password&#xff1a;这个是root的密码 Retype new …

@property

class Person(object):def __init__(self, name,age):#属性直接对外暴露#self.age age#限制访问self.__age ageself.__name namedef getAge(self):return self.__agedef setAge(self,age):if age<0:age 0self.__age age#方法名为受限制的变量去掉双下划线propertydef a…

ubuntu入门知识

1、linux系统发展历史 unix -> Linux -> ubuntu linux发展轨迹图 2、ubuntu下载和安装 推荐使用长期支持版本&#xff1a; 10.04,12.04,14.04或LTS版本 安装环境VMware虚拟机 3、安装之后创建root sudo passwd root 输入root用户密码即可 4、安装软件&#xff1a; 更新软…

html 二级试题,计算机二级考试WEB试题及答案

计算机二级考试WEB试题及答案当前主要的 WEB数据库访问技术有哪些?答&#xff1a;到目前为止&#xff0c;WEB数据库访问技术主要分为两大类&#xff1a;(1)公共网关接口技术(CGI);CGI 是 WEB 服务器运行时外部程序的规范&#xff0c;按照 CGI 编写的程序可以扩展服务器的功能&…