package com.yztc_popupwindow;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;
/**
* 演示PopupWindow
*/
public class PopupWindowActivity extends Activity implements OnClickListener{
private View contentView;
private PopupWindow mWindow;
private ImageView ivBack, ivCollect, ivShare;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建PopupWimdow
initPopupWindow();
// 三个按钮点击事件
initView();
}
private void initView() {
ivBack = (ImageView) contentView.findViewById(R.id.iv_back);
ivCollect = (ImageView) contentView.findViewById(R.id.iv_collect);
ivShare = (ImageView) contentView.findViewById(R.id.iv_share);
ivBack.setOnClickListener(this);
ivCollect.setOnClickListener(this);
ivShare.setOnClickListener(this);
}
private void initPopupWindow() {
// contentView:内容视图
contentView = getLayoutInflater()
.inflate(R.layout.content_layout, null);
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键
// isShowing():判断PopupWindow是否正在展示
if (mWindow.isShowing()) {
mWindow.dismiss();// 消失
} else {
// 显示PopupWindow
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.iv_back:
finish();
if (mWindow.isShowing()) {
mWindow.dismiss();
}
break;
case R.id.iv_collect:
Toast.makeText(this, "收藏", Toast.LENGTH_SHORT).show();
break;
case R.id.iv_share:
Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (mWindow != null) {
mWindow.dismiss();
mWindow = null;
}
}
}
PopupWindow布局
<?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="horizontal" >
<ImageView
android:id="@+id/iv_back"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/iv_collect"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/iv_share"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
</LinearLayout>