废话少说 先上效果图
奥利给!
不是这种布局效果的可以自己写
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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_20"
android:layout_marginRight="@dimen/dp_20"
android:background="@drawable/shape15"
android:orientation="vertical">
<TextView
android:id="@+id/xj_pop"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50"
android:gravity="center"
android:text="@string/xj"
android:textColor="@color/colorBlack"></TextView>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_1"
android:layout_marginLeft="@dimen/dp_20"
android:layout_marginRight="@dimen/dp_20"
android:background="@color/colorGray"></View>
<TextView
android:id="@+id/xc_pop"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50"
android:gravity="center"
android:text="@string/xc"
android:textColor="@color/colorBlack"></TextView>
</LinearLayout>
<TextView
android:id="@+id/cancel_pop"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50"
android:layout_marginLeft="@dimen/dp_20"
android:layout_marginTop="@dimen/dp_5"
android:layout_marginRight="@dimen/dp_20"
android:layout_marginBottom="@dimen/dp_5"
android:background="@drawable/shape15"
android:gravity="center"
android:text="@string/cancel"
android:textColor="@color/colorBlack"></TextView>
</LinearLayout>
自定义布局的popupWindow
View inflater = LayoutInflater.from(this).inflate(R.layout.pop_layout, null);
TextView xj_pop = inflater.findViewById(R.id.xj_pop);
TextView xc_pop = inflater.findViewById(R.id.xc_pop);
TextView cancel_pop = inflater.findViewById(R.id.cancel_pop);
popupWindow = new PopupWindow(inflater, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(null);
popupWindow.setAnimationStyle(R.style.PopAnimation);//动画效果
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
// 设置背景颜色变暗
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.7f;
getWindow().setAttributes(lp);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
}
});
/**
* POP的取消按钮
*/
cancel_pop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
//popupWindow在屏幕下方展示
popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, -50);//‘view’是点击事件方法里的
动画效果(在res文件夹下自己新建文件夹:anim)
//pop_cancel.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="500"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0" android:duration="500"/>
</set>
//pop_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="400"/>
<alpha android:fromAlpha="0" android:toAlpha="1.0" android:duration="400"/>
</set>
最后在values文件夹下的style的页面里添加:
<!-- PopupWindow 的动画效果 -->
<style name="PopAnimation">
<item name="android:windowExitAnimation">@anim/pop_cancel</item>
<item name="android:windowEnterAnimation">@anim/pop_in</item>
</style>
OK 大功告成~