PopupWindow浮动窗
来源:互联网 发布:java中怎么定义数组 编辑:程序博客网 时间:2024/05/22 02:09
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(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
由上面方法可以看出,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; } }}
三、效果图
- PopupWindow浮动窗
- popupwindow(浮动窗体的实现)
- PopupWindow组件实现浮动窗口
- Android实战简易教程-第七十二枪(PopupWindow浮动窗)
- PopupWindow---Android PopupWindow弹窗教程
- PopupWindow//弹出窗---未完
- PopupWindow弹窗事件
- PopupWindow弹窗事件
- PopupWindow 定点弹窗
- popupwindow 弹窗效果图
- popupwindow
- popupWindow
- PopupWindow
- PopupWindow
- PopupWindow
- PopupWindow
- PopupWindow
- popupwindow
- org.apache.commons.beanutils.BeanUtils 工具类详解
- Http基本的错误码
- 程序人生 重新出发-勇敢迈入程序猿的世界
- net_device剖析
- Oracle Study之-Oracle 11g OCM考试大纲
- PopupWindow浮动窗
- 游戏之帮组策划,设计策划填表方法
- C函数
- 一些sql的写法
- 时间问题,增加n秒n分n小时
- 加密类型
- [android]一个关于UDP和TCP的项目实践(二)
- vc资源 Windows资源 文件资源 自定义文件资源的读取
- 【学习笔记】3D图形核心基础精炼版-6:stage3D实战-顶点