PopupWindow

*** PopupWindow

*显示类型:
下拉式
mWindow.showDropDown(anchor,xoff,yoff,gravity);
浮动式
mWindow.showAtlocation(ContentView,Gravity,BOOTTOM,0,0)

案例效果:点击菜单按钮时若PopuWindow未显示则让它从底部显示出来,若已显示则让它消失,点击返回键时若PopupWindow正在显 示则让它消失;点击收藏和分享选项时Toast显示,点击退出选项时若PopupWindow正在显示则让它消失并结束活动

content_layout.xml文件中创建Popup显示时的选项:

<ImageView 
  android:id="@+id/iv1"
  android:layout_width="0dp"
  android:layout_height="50dp"
  android:layout_weight="1"
  android:scaleType="fitXY"
  android:src="@drawable/contentback"
  />
<ImageView 
  android:id="@+id/iv2"
  android:layout_width="0dp"
  android:layout_height="50dp"
  android:layout_weight="1"
  android:scaleType="fitXY"
  android:src="@drawable/contentshare"
  />
<ImageView 
  android:id="@+id/iv3"
  android:layout_width="0dp"
  android:layout_height="50dp"
  android:layout_weight="1"
  android:scaleType="fitXY"
  android:src="@drawable/contentcollect"
  />

MainActivity.java中:

public class MainActivity extends Activity implements OnClickListener{
	private View contentView;
	private PopupWindow mWindow;
	private ImageView iv1,iv2,iv3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initPopuWindow();
		initView();
	}

	private void initView() {
		iv1 = (ImageView) contentView.findViewById(R.id.iv1);
		iv2 = (ImageView) contentView.findViewById(R.id.iv2);
		iv3 = (ImageView) contentView.findViewById(R.id.iv3);
		
		iv1.setOnClickListener(this);
		iv2.setOnClickListener(this);
		iv3.setOnClickListener(this);	
	}

	private void initPopuWindow() {
		//将xml布局文件转换成我们需要的内容分视图
		contentView = getLayoutInflater().inflate(R.layout.content_layout, null);
		//第一个参数contentView参数:是内容视图
		mWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		//设置触摸PopupWindow外面的区域时是否可以使得这个PopupWindow消失
		mWindow.setOutsideTouchable(true);
		//设置背景
		//mWindow.setBackgroundDrawable(background);
		//设置PopupWindow是否触摸时会有响应
		//mWindow.setTouchable(true);
		
	}
	//重写专门处理某个按键的方法
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_MENU://监听menu键
			if(mWindow.isShowing()){//isShowing():判断PoupWindow是否正在显示
				mWindow.dismiss();//dismiss():让popupWindow消失的方法
			}else{
				//显示PoupWindow--->在某个位置显示
				mWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
				//作为下拉视图显示
				//mWindow.showAsDropDown(anchor, xoff, yoff);
			}
			break;
		case KeyEvent.KEYCODE_BACK://监听返回键
			if(mWindow.isShowing()){
				mWindow.dismiss();
			}
			break;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv1:
			finish();
			if(mWindow.isShowing()){
				mWindow.dismiss();
			}
			break;
		case R.id.iv2:
			Toast.makeText(this, "收藏", Toast.LENGTH_SHORT).show();
			break;
		case R.id.iv3:
			Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show();
			break;
		}	
	}
	@Override
	protected void onDestroy() { //PopupWindow不用时将它的对象给回收掉
		super.onDestroy();
		if(mWindow!=null){
			mWindow.dismiss();
			mWindow = null;
		}
	}
}

原文地址:https://www.cnblogs.com/SanguineBoy/p/9802852.html