PopupWindow浮动窗
2016-04-08 16:52
513 查看
PopupWindow的官方定义如下:
A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current
activity.
也就是说它是一个浮动在当前界面上方并且可以显示在任意位置的View,前面的章节我们学习了弹出框,各式各样的,那么PopupWindow应该有两点和弹出框不同,一是PopupWindow必须指定宽高属性,而弹出框则不是必须指定;二是PopupWindow必须指定其布局文件。下面总结一下常用方法:
由上面方法可以看出,PopupWindow共有两种弹出方式,一种在某控件的下方,一种在父控件的任意方向,弹出位置可以任意设定,十分灵活。
一、布局文件如下
1.主布局文件
二、逻辑代码
三、效果图
A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current
activity.
也就是说它是一个浮动在当前界面上方并且可以显示在任意位置的View,前面的章节我们学习了弹出框,各式各样的,那么PopupWindow应该有两点和弹出框不同,一是PopupWindow必须指定宽高属性,而弹出框则不是必须指定;二是PopupWindow必须指定其布局文件。下面总结一下常用方法:
方法 | 类型 | 说明 |
PopupWindow(Context context) | 构造方法 | Context为上下文对象 |
PopupWindow(int width, int height) | 构造方法 | 传入宽高属性 |
PopupWindow(View contentView, int width, int height) | 构造方法 | 传入布局文件及宽高属性 |
PopupWindow(View contentView, int width, int height, boolean focusable) | 构造方法 | 传入布局文件、宽高属性和是否获得焦点 |
dismiss() | 普通方法 | 隐藏PopupWindow |
setAnimationStyle(int animationStyle) | 普通方法 | 为PopupWindow添加动画 |
setBackgroundDrawable(Drawable background) | 普通方法 | 改变PopupWindow的背景 |
setContentView(View contentView) | 普通方法 | 为PopupWindow设置布局 |
setFocusable(boolean focusable) | 普通方法 | 是否获取焦点 |
setOnDismissListener(PopupWindow. OnDismissListener onDismissListener) | 普通方法 | 设置PopupWindow消失监听 |
showAsDropDown(View anchor, int xoff, int yoff, int gravity) | 普通方法 | 在某控件下方弹出PopupWindow |
showAtLocation(View parent, int gravity, int x, int y) | 普通方法 | 在父控件的什么位置弹出PopupWindow |
一、布局文件如下
1.主布局文件
<LinearLayout 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:orientation="vertical" > <Button android:onClick="show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="从底部显示" /> <ImageView android:id="@+id/image" android:onClick="showImage" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/ic_add_member"/> </LinearLayout>2.两个popubWindow的布局文件
<?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" > <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#10a324" /> <TextView android:padding="6dp" android:id="@+id/tv_take" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#29a0cc" android:gravity="center" android:text="拍照" android:textColor="#ff0000" android:textSize="20sp" /> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#10a324" /> <TextView android:id="@+id/select" android:padding="6dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#29a0cc" android:gravity="center" android:text="相册选择" android:textColor="#ff0000" android:textSize="20sp" /> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#10a324" /> <TextView android:id="@+id/exeit" android:padding="6dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#29a0cc" android:gravity="center" android:text="立即退出" android:textColor="#ff0000" android:textSize="20sp" /> </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#24a943" android:orientation="vertical" > <LinearLayout android:id="@+id/ll_pic" android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" > <ImageView android:layout_width="35dp" android:layout_height="35dp" android:padding="5dp" android:src="@drawable/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="35dp" android:gravity="center" android:paddingLeft="20dp" android:paddingRight="40dp" android:text="图片" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" > <ImageView android:layout_width="35dp" android:layout_height="35dp" android:padding="5dp" android:src="@drawable/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="35dp" android:gravity="center" android:paddingLeft="20dp" android:paddingRight="40dp" android:text="GPS" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" > <ImageView android:layout_width="35dp" android:layout_height="35dp" android:padding="5dp" android:src="@drawable/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="35dp" android:gravity="center" android:paddingLeft="20dp" android:paddingRight="40dp" android:text="音乐" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" > <ImageView android:layout_width="35dp" android:layout_height="35dp" android:padding="5dp" android:src="@drawable/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="35dp" android:gravity="center" android:paddingLeft="20dp" android:paddingRight="40dp" android:text="WIFI" /> </LinearLayout> </LinearLayout>
二、逻辑代码
package com.example.mydemo; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private PopupWindow popupWindow; private View rootView; private ImageView imageView; private PopupWindow popupWindow1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.image); } /** * 显示在指定控件的下部 * @param v */ public void showImage(View v){ if(popupWindow1==null){ View view= LayoutInflater.from(this).inflate(R.layout.pop,null);//获取popupWindow子布局对象 popupWindow1 =new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,false);//初始化 popupWindow1.showAsDropDown(imageView,-300,0);//在ImageView控件下方弹出 //popupWindow.setAnimationStyle(R.style.popupAnim);//设置动画 }else { popupWindow1.showAsDropDown(imageView,-300,0); } } /** * 显示在底部 * @param v */ public void show(View v){ //这里要进行判断,防止重复显示popupWindow if(popupWindow==null){ showPopupWindow(); }else { popupWindow.showAtLocation(rootView, Gravity.BOTTOM,0,0);//设置PopupWindow的弹出位置。 } } private void showPopupWindow() { View view = LayoutInflater.from(this).inflate(R.layout.popupwindow,null);//PopupWindow对象 popupWindow = new PopupWindow(this); popupWindow.setContentView(view);//设置PopupWindow布局文件 popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);//设置PopupWindow宽 popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);//设置PopupWindow高 rootView = LayoutInflater.from(this).inflate(R.layout.activity_main, null); popupWindow.showAtLocation(rootView, Gravity.BOTTOM,0,0); popupWindow.setOutsideTouchable(true); //从popupWindow 布局中找到控件,并设置点击事件 TextView tv_take=(TextView) view.findViewById(R.id.tv_take); TextView tv_select=(TextView) view.findViewById(R.id.select); TextView tv_exeit=(TextView) view.findViewById(R.id.exeit); tv_take.setOnClickListener(this); tv_select.setOnClickListener(this); tv_exeit.setOnClickListener(this); popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { Toast.makeText(MainActivity.this,"PupWindow消失了!",Toast.LENGTH_SHORT).show(); } }); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.tv_take: Toast.makeText(MainActivity.this,"就要进行拍照了",Toast.LENGTH_SHORT).show(); break; case R.id.select: Toast.makeText(MainActivity.this,"选择照片",Toast.LENGTH_SHORT).show(); break; case R.id.exeit: popupWindow.dismiss();//关闭PopupWindow break; default: break; } } }
三、效果图
相关文章推荐
- 6.1.2Linux下Socket编程
- Linux/UNIX线程(2)
- [转载]Linux下tomcat的catalina.out文件过大,以及目录更改解决办法
- 在CentOS上安装Java环境:使用yum安装java
- ecshop -爱T-blog
- linux添加网关和出接口不同网段的路由
- CentOS下载
- Hadoop-与RDBMS
- Tomcat工程发布到linux系统上
- VMware虚拟的CENTOS系统中挂在CD/DVD驱动
- 为odoo安装反向代理
- Linux内核及分析 第七周 可执行程序的装载
- Linux 应用编程
- linux之NAND FLASH驱动程序
- linux之keepalived详解
- opencv imwrite()函数 崩溃 失败
- OpenMP中几个容易混淆的函数(线程数量/线程ID/线程最大数)以及并行区域线程数量的确定
- tomcat启动报错:IOException while loading persisted sessions: java.io.EOFException java.io.EOFException
- tomcat 默认网站和虚拟目录配置
- [原创] ubuntu下安装scrapy报错 error: command 'x86_64-linux-gnu-gcc' failed with exit status 1