在有些场景中,需要实现屏幕底部弹出对话框效果,这个效果可以用dialog和popupWindow实现。
1.采用dialog实现
public class MyDialogy extends Dialog {
public MyDialogy(Context context) {
super(context, R.style.dialogy);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_dialog_selectphoto);
setCanceledOnTouchOutside(true);
setCancelable(true);
Window window = getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.gravity = Gravity.BOTTOM;
lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);
findViewById(R.id.tv_cancel).setOnClickListener(new mClickListener());
findViewById(R.id.tv_xc).setOnClickListener(new mClickListener());
findViewById(R.id.tv_pz).setOnClickListener(new mClickListener());
}
private class mClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_pz:
listener.getPhoto_xj();
dismiss();
break;
case R.id.tv_xc:
listener.getPhoto_xc();
dismiss();
break;
case R.id.tv_cancel:
listener.cancel();
dismiss();
break;
default:
break;
}
}
}
public interface DialogyItemClickListener {
void getPhoto_xc();
void getPhoto_xj();
void cancel();
}
DialogyItemClickListener listener;
public void setOnDialogyItemListener(DialogyItemClickListener listener) {
this.listener = listener;
}
}
在activity里面直接通过创建Mydialog对象,通过
setOnDialogyItemListener方法进行监听dialog里面的点击事件。
2.采用popupWindow实现
PopupWindow popupWindow = new PopupWindow(view,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setAnimationStyle(R.style.MyPopupWindow_anim_style);//popupWindow动画
popupWindow.setFocusable(true);//点击空白,隐藏popupWindow
setBackgroundAlpha(0.5f);
popupWindow.showAtLocation(rootView, Gravity.BOTTOM | Gravity.CENTER, 0,
0);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
setBackgroundAlpha(1.0f);
}
});其中view是activity布局的根目录,同时对popupWindow弹出时对背景设置了透明度,同时添加了popupWindow的弹出动画。
<style name="MyPopupWindow_anim_style">
<item name="android:windowEnterAnimation">@anim/popup_in_anim</item>
<item name="android:windowExitAnimation">@anim/popup_out_anim</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="250"
android:fromYDelta="100.0%"
android:toYDelta="0.0" />
</set>