初始化
mPopupWindow=new PopupWindow(getContext());
pop显示
private void showPop() {
TextView tv_redu, tv_time, tv_rank, tv_share;
mPopupWindow.setContentView(LayoutInflater.from(getContext()).inflate(R.layout.pop_add, null));
// 为了避免部分机型不显示,我们需要重新设置一下宽高
mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
// 设置pop透明效果
mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x0000));
// 设置pop出入动画
mPopupWindow.setAnimationStyle(R.style.pop_add);
// 设置pop获取焦点,如果为false点击返回按钮会退出当前Activity,如果pop中有Editor的话,focusable必须要为true
mPopupWindow.setFocusable(true);
// 设置pop可点击,为false点击事件无效,默认为true
mPopupWindow.setTouchable(true);
// 设置点击pop外侧消失,默认为false;在focusable为true时点击外侧始终消失
mPopupWindow.setOutsideTouchable(true);
// 相对于 + 号正下面,同时可以设置偏移量
mPopupWindow.showAsDropDown(txt_order, 0, 0);
// 设置pop关闭监听,用于改变背景透明度
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
//toggleBright();
}
});
tv_redu = mPopupWindow.getContentView().findViewById(R.id.tv_redu);
tv_time = mPopupWindow.getContentView().findViewById(R.id.tv_time);
tv_rank = mPopupWindow.getContentView().findViewById(R.id.tv_rank);
tv_share = mPopupWindow.getContentView().findViewById(R.id.tv_share);
tv_redu.setOnClickListener(this);
tv_time.setOnClickListener(this);
tv_rank.setOnClickListener(this);
tv_share.setOnClickListener(this);
}
打开关闭的样式
<style name="pop_add">
<item name="android:windowEnterAnimation">@anim/pop_add_show</item>
<item name="android:windowExitAnimation">@anim/pop_add_hide</item>
</style>
pop_add_show
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
<scale
android:duration="500"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="85%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="1.0"/>
</set>
pop_add_hide
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.0"/>
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="85%"
android:pivotY="0%"
android:toXScale="0"
android:toYScale="0"/>
</set>
pop_add
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center"
>
<TextView
android:id="@+id/tv_redu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="热度排序"
android:padding="5dp"
/>
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="时间排序" />
<TextView
android:id="@+id/tv_rank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="等级排序" />
<TextView
android:id="@+id/tv_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="分享排序" />
</LinearLayout>