底部dialog与底部popupWindow

在有些场景中,需要实现屏幕底部弹出对话框效果,这个效果可以用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>



                



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值