博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android UI:PopupWindow基本学习
阅读量:4132 次
发布时间:2019-05-25

本文共 3642 字,大约阅读时间需要 12 分钟。

基本使用

什么是PopupWindow?

就是点击某个控件,弹出个view,弹出的view就是PopupWindow
效果图:点击button,弹出PopWindow,里面又有4个button,点击第一个button,弹出toast。

popwindow对应的布局

3 代码

package com.examp.popupwindowdemo;import android.os.Bundle;import android.app.Activity;import android.graphics.Color;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.ColorDrawable;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.LinearLayout;import android.widget.PopupWindow;import android.widget.Toast;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button btn=(Button) findViewById(R.id.btn);        btn.setOnClickListener(new OnClickListener() {						@Override			public void onClick(View v) {				//打开popupwindow				showPopupWindow(v);			}		});    }    protected void showPopupWindow(View v) {		View contentView = View.inflate(getApplicationContext(), R.layout.popwindow, null);		PopupWindow popupWindow = new PopupWindow(contentView, 				LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);		popupWindow.setTouchable(true);//popupwindow可点击		popupWindow.setOutsideTouchable(true);//popupwindow以外的地方可点击,使其调用dismiss()方法,不设置这个点击popupwindow以外的区域pop不会消失		popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//必须设置Drawable背景(2中方法)//		popupWindow.setBackgroundDrawable(new  BitmapDrawable());		Button btn_pop=(Button) contentView.findViewById(R.id.btn_pop);		btn_pop.setOnClickListener(new OnClickListener() {						@Override			public void onClick(View v) {				Toast.makeText(getApplicationContext(), "pop弹出toast", Toast.LENGTH_LONG).show();			}		});		popupWindow.showAsDropDown(v);	}}

注意

必须设置BackgroundDrawable

必须给popupWindow设置BackgroundDrawable,否则setFocusable(true)无效
popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
//popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//(2中方法)//popupWindow.setBackgroundDrawable(new  BitmapDrawable());

设置popupwindow的显示位置

第一种:

int[] locations = new int[2];
btn.getLocationInWindow(locations);//Gravity.TOP|Gravity.LEFT
popupWindow.showAtLocation(btn,Gravity.TOP, locations[0], locations[1]);第二种:
//方式二:显示在View下方//没有偏移量popupWindow.showAsDropDown(view);
//有偏移量:后2个参数是x y轴偏移量popupWindow.showAsDropDown(view, 100, 0);

创建PopupWindow的工具类

public class WindowUtils {	/**	 * 显示弹出视图	 * @param context	 * @param positionView	 * @param view	 * @param x	 * @param y	 * @return	 */	public static PopupWindow showPopWindow(Context context, View positionView, View view, int x, int y) {		// 创建 PopupWindow		// PopupWindow popwindow=new PopupWindow(视图对象 , 宽, 高);		PopupWindow popwindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);				//在视图对象 以外的范围 点击关闭		popwindow.setOutsideTouchable(true);//自动关闭				popwindow.setFocusable(true);//响应返回键关闭1.聚集 2.背景		popwindow.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));		// 指定控件做参考 显示出视图		// popwindow.showAtLocation(参考视图, 坐标位置, 偏移x,偏移y)		// int [] location =new int[2];		// positionView.getLocationOnScreen(location);//获取视图在屏幕中的坐标		popwindow.showAtLocation(positionView, Gravity.LEFT | Gravity.TOP, x, y);				return popwindow;	}}

dismiss()无效的原因

添加如下代码

popupWindow.setTouchable(true);popupWindow.setFocusable(true);popupWindow.setOutsideTouchable(true);

 
 
 

其它弹框

其他弹框

123

11111

3333333333

222222222

你可能感兴趣的文章
实例区别BeanFactory和FactoryBean
查看>>
Spring后置处理器BeanPostProcessor的应用
查看>>
Mysql中下划线问题
查看>>
微信小程序中使用npm过程中提示:npm WARN saveError ENOENT: no such file or directory
查看>>
Xcode 11 报错,提示libstdc++.6 缺失,解决方案
查看>>
idea的安装以及简单使用
查看>>
Windows mysql 安装
查看>>
python循环语句与C语言的区别
查看>>
Vue项目中使用img图片和background背景图的使用方法
查看>>
vue 项目中图片选择路径位置static 或 assets区别
查看>>
vue项目打包后无法运行报错空白页面
查看>>
Vue 解决部署到服务器后或者build之后Element UI图标不显示问题(404错误)
查看>>
element-ui全局自定义主题
查看>>
facebook库runtime.js
查看>>
vue2.* 中 使用socket.io
查看>>
openlayers安装引用
查看>>
js报错显示subString/subStr is not a function
查看>>
高德地图js API实现鼠标悬浮于点标记时弹出信息窗体显示详情,点击点标记放大地图操作
查看>>
初始化VUE项目报错
查看>>
vue项目使用安装sass
查看>>