安卓开发笔记——PopupWindow,做出如弹出框效果
2016-03-29 00:50
363 查看
先看一个效果图
点击按钮后出现一个这么的效果,这个弹出框实现的答题代码如下
先来一个弹出框的布局xml
然后是在主界面的设计,按钮自己设定,我把在按钮内如何引入写出来
代码如下
代码里的解释已经很详尽了,这也是我学着写的 ,然后弹出框的点击事件这样写就可以了,千万不要在xml里直接使用android:onClick=“click1”这样,然后直接在主界面里,用
public void click1(View v){}这样写,因为view指的并不是弹出框的布局xml,因此不会做出响应
点击按钮后出现一个这么的效果,这个弹出框实现的答题代码如下
先来一个弹出框的布局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:orientation="vertical" android:background="#90EE90" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:text="请选择" android:textColor="#ff2525" android:textSize="20sp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="#969696" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:onClick="abc1" android:gravity="left|center_vertical" android:padding="10dp" android:text="个人信息" android:textColor="#2a2a2a" android:textSize="15sp" /> <View android:layout_width="match_parent" android:layout_height="1px" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="#a6a6a6" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:onClick="abc2" android:gravity="left|center_vertical" android:padding="10dp" android:text="好友列表" android:textColor="#2a2a2a" android:textSize="15sp" /> <View android:layout_width="match_parent" android:layout_height="1px" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="#a6a6a6" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:onClick="abc2" android:gravity="left|center_vertical" android:padding="10dp" android:text="我的留言板" android:textColor="#2a2a2a" android:textSize="15sp" /> </LinearLayout> </LinearLayout>
然后是在主界面的设计,按钮自己设定,我把在按钮内如何引入写出来
代码如下
LayoutInflater inflater = LayoutInflater.from(getBaseContext());//获取一个填充器 View view = inflater.inflate(R.layout.chakan, null);//填充我们自定义的布局 Display display = getWindowManager().getDefaultDisplay();//得到当前屏幕的显示器对象 Point size = new Point();//创建一个Point点对象用来接收屏幕尺寸信息 display.getSize(size);//Point点对象接收当前设备屏幕尺寸信息 int width = size.x;//从Point点对象中获取屏幕的宽度(单位像素) int height = size.y;//从Point点对象中获取屏幕的高度(单位像素) Log.v("zxy", "width="+width+",height="+height);//width=480,height=854可知手机的像素是480x854的 //创建一个PopupWindow对象,第二个参数是设置宽度的,用刚刚获取到的屏幕宽度乘以2/3,取该屏幕的2/3宽度,从而在任何设备中都可以适配,高度则包裹内容即可,最后一个参数是设置得到焦点 PopupWindow popWindow = new PopupWindow(view, 2*width/3, LayoutParams.WRAP_CONTENT, true); popWindow.setBackgroundDrawable(new BitmapDrawable());//设置PopupWindow的背景为一个空的Drawable对象,如果不设置这个,那么PopupWindow弹出后就无法退出了 popWindow.setOutsideTouchable(true);//设置是否点击PopupWindow外退出PopupWindow WindowManager.LayoutParams params = getWindow().getAttributes();//创建当前界面的一个参数对象 params.alpha = 1f;//设置参数的透明度为0.8,透明度取值为0~1,1为完全不透明,0为完全透明,因为android中默认的屏幕颜色都是纯黑色的,所以如果设置为1,那么背景将都是黑色,设置为0,背景显示我们的当前界面 getWindow().setAttributes(params);//把该参数对象设置进当前界面中 Button btn = (Button) view.findViewById(R.id.button1); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Intent intent=new Intent(user.this,persondata.class); startActivity(intent); finish(); } }); Button btn1 = (Button) view.findViewById(R.id.button2); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg1) { Intent intent=new Intent(user.this,friendlist.class); startActivity(intent); finish(); } }); Button btn2 = (Button) view.findViewById(R.id.button3); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg1) { Intent intent=new Intent(user.this,liuyanban.class); startActivity(intent); finish(); } }); popWindow.setOnDismissListener(new OnDismissListener() {//设置PopupWindow退出监听器 @Override public void onDismiss() {//如果PopupWindow消失了,即退出了,那么触发该事件,然后把当前界面的透明度设置为不透明 WindowManager.LayoutParams params = getWindow().getAttributes(); params.alpha = 1.0f;//设置为不透明,即恢复原来的界面 getWindow().setAttributes(params); } }); //第一个参数为父View对象,即PopupWindow所在的父控件对象,第二个参数为它的重心,后面两个分别为x轴和y轴的偏移量 popWindow.showAtLocation(inflater.inflate(R.layout.addfriend_layout, null), Gravity.CENTER, 0, 0);
代码里的解释已经很详尽了,这也是我学着写的 ,然后弹出框的点击事件这样写就可以了,千万不要在xml里直接使用android:onClick=“click1”这样,然后直接在主界面里,用
public void click1(View v){}这样写,因为view指的并不是弹出框的布局xml,因此不会做出响应
相关文章推荐
- Linux学习三部曲(之二)
- apache2服务器mod_rewrite模块 开启方法[linux, ubuntu]
- MFC中快速应用OpenCV
- 部署点评Cat监控项目
- CAT监控平台之装配篇
- Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:2.10.1:jar (attach-javadocs) on
- Apache开源库BeanUtils操作JavaBean的属性
- CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境
- vs 2013---error C4996: 'fopen':
- 传统店家做电商不能只靠淘宝
- Tomcat和JBoss都是服务器,有什么区别呢
- 知道什么网站并发量吗?如何测试呢?这个必须了解了解。。。。
- linux下PHP7环境搭建
- tomcat8开启远程debug
- shell ftp 自动上传下载文件
- 你知道Nginx的七大优势吗?
- solr+tomcat+nutch 整合
- Nginx 配置实践
- ecshop登录超时、session过期的解决办法
- 三种方法实现ecshop购物车退出不清空