样式:
layout:
popup_appinfo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pop_bg"
android:orientation="horizontal" > <TextView
android:id="@+id/tv_uninstall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:drawablePadding="3dp"
android:drawableTop="@drawable/ic_uninstall"
android:text="卸载"
android:textColor="@color/black"
android:textSize="16sp" /> <TextView
android:id="@+id/tv_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:drawablePadding="3dp"
android:drawableTop="@drawable/ic_open"
android:text="打开"
android:textColor="@color/black"
android:textSize="16sp" /> <TextView
android:id="@+id/tv_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:drawablePadding="3dp"
android:drawableTop="@drawable/ic_share"
android:text="分享"
android:textColor="@color/black"
android:textSize="16sp" /> <TextView
android:id="@+id/tv_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:drawablePadding="3dp"
android:drawableTop="@drawable/ic_info"
android:text="信息"
android:textColor="@color/black"
android:textSize="16sp" /> </LinearLayout>
anim:
popup_enter.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" > <translate
android:duration="400"
android:fromXDelta="100%p"
android:interpolator="@android:interpolator/overshoot"
android:toXDelta="0" /> <alpha
android:duration="400"
android:fromAlpha="0.2"
android:toAlpha="1.0" /> </set>
popup_exit.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" > <translate
android:duration="400"
android:fromXDelta="0"
android:interpolator="@android:interpolator/anticipate"
android:toXDelta="50%p" /> <alpha
android:duration="400"
android:fromAlpha="1.0"
android:toAlpha="0" /> </set>
code:
Activity.java
......
//显示popupwindow
protected void showPopup(View itemView) {
if (mPopupWindow == null) {//只需要初始化一次弹窗
//初始化弹窗布局
View view = View.inflate(this, R.layout.popup_appinfo, null); //设置按钮点击事件
TextView tvUninstall = (TextView) view
.findViewById(R.id.tv_uninstall);
TextView tvOpen = (TextView) view.findViewById(R.id.tv_open);
TextView tvShare = (TextView) view.findViewById(R.id.tv_share);
TextView tvInfo = (TextView) view.findViewById(R.id.tv_info);
tvUninstall.setOnClickListener(this);
tvOpen.setOnClickListener(this);
tvShare.setOnClickListener(this);
tvInfo.setOnClickListener(this); //初始化弹窗对象
////参1:布局; 参2,3:宽高; 参4:获取焦点
mPopupWindow = new PopupWindow(view,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, true); //设置背景;只有设置了背景,点击窗口外侧和返回键,弹窗才会消失
mPopupWindow.setBackgroundDrawable(new ColorDrawable()); //设置动画样式
mPopupWindow.setAnimationStyle(R.style.PopupAnim);
} //显示在item正下方, 然后向上偏移一个item的高度
mPopupWindow.showAsDropDown(itemView, 60, -itemView.getHeight());
}
......
//弹窗消失
mPopupWindow.dismiss();
......