ダイアログとpopupwindowの比較

1.同じ2点

1.1ダイアログボックスを設定することができます
1.2は、ポップ様々なスタイルを想定して設定することができます

2.両者の差

本質的な違い:

ダイアログが非ブロックダイアログボックスで、背景には、他のことを行うことができるときAlertDialogポップ;
popupwindow待ち時間は、プログラムがpopupwindowの終わりまで待つことになるときpopupwindowダイアログボックスをブロックして、プログラムがポップアップ表示されます、プログラム近いポップメソッドpopupwindowを閉じ呼び出すときダウンしました

他の違いは:
1.Popが表示幅と高さ、ダイアログ無制限の前に設定する必要があり
、ダイアログは、ポップデフォルトの応答を消えます2. [バック(あなたがtrueに(popup.setFocusableを設定していない場合))
3.Popは、ページにデフォルト設定はありませんマスク層、およびレイヤダイアログのデフォルトモンゴルの残りの部分を追加しない
4.PopupWindowノータイトルを、デフォルトのタイトルのダイアログがあり
、両方のディスプレイが重力を設定する必要があります5.。設定されていない場合は、ダイアログのデフォルトGravity.CENTER
6.両方のデフォルトの背景を持って、透明setBackgroundDrawable(新新ColorDrawable(android.R.color.transparent))によって提供することができる
7.Dialogは(番組で)ポップアップウィンドウが表示されます。 showAtLocation()ポップでポップ

3.コードの実装

ダイアログがポップアップの下部を達成します

public class BottomDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private TextView tvCall;
    private String name;
    private String phone_number;
    private TextView tv_contact_customer;
    private TextView tv_cancel;

    public BottomDialog(@NonNull Context context, String name, String phone_number, @StyleRes int themeResId){
        super(context, themeResId);
        this.context = context;
        this.name = name;
        this.phone_number = phone_number;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_contact_phone_dialog);//弹窗布局
        if (this.getWindow() != null) {
            WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
            layoutParams.gravity = Gravity.BOTTOM;//底部弹出
            layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
            layoutParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
            getWindow().getDecorView().setPadding(0, 0, 0, 0);
            getWindow().setAttributes(layoutParams);
            //getWindow().setWindowAnimations(R.style.DialogAnimation);//view中添加动画
        }
        tvCall = findViewById(R.id.call_service);
        tv_contact_customer = findViewById(R.id.tv_contact);
        tv_cancel = findViewById(R.id.tv_close);
        tv_cancel.setOnClickListener(this);
        tv_contact_customer.setOnClickListener(this);
        tvCall.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.call_service) {
            if (onCallSalesListener != null) {
                onCallSalesListener.onCallSalesman();
            }
        }
        if (view.getId() == R.id.tv_contact) {
            if (onCallSalesListener != null) {
                onCallSalesListener.onContactCustomerService();
            }
        }
        if (view.getId() == R.id.tv_close) {
            dismiss();
        }
    }

    //监听接口
    public interface OnCallSalesListener {
        void onCallSalesman();

        void onContactCustomerService();
    }

    private OnCallSalesListener onCallSalesListener;

    public void setOnCallSalesListener(OnCallSalesListener onCallSalesListener) {
        this.onCallSalesListener = onCallSalesListener;
    }
    
}

コール:

 BottomDialog bottomDialog = new BottomDialog(this, "王老五","110", R.style.AlertDialogStyle);
 bottomDialog.show();

popupwindowポップの下を達成

public class BottomPopWindow extends PopupWindow implements View.OnClickListener{


    private View mMenuView;
    //文本信息
    private TextView tv_prompt_info;
    private final RelativeLayout rl_close;
    private final TextView tv_close;

    public BottomPopWindow(final Context context) {
        super(context);
        mMenuView = View.inflate(context,R.layout.layout_bottom_pop,null);
        //提示信息内容
        tv_prompt_info = mMenuView.findViewById(R.id.tv_info);
        rl_close = mMenuView.findViewById(R.id.rl_close);
        rl_close.setOnClickListener(this);
        tv_close = mMenuView.findViewById(R.id.tv_close);
        tv_close.setOnClickListener(this);
        tv_prompt_info.setText("寒塘渡鹤影,冷月葬花魂");
        tv_prompt_info.setHighlightColor(context.getResources().getColor(android.R.color.transparent));
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);//设置布局
        //设置弹出窗体的宽和高
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //返回键可点击
        this.setFocusable(true);
        //外部可点
        this.setOutsideTouchable(true);
        //弹出窗体的背景
        this.setBackgroundDrawable(new ColorDrawable(0xAA000000));
        //动画效果
        this.setAnimationStyle(R.style.DialogAnimation);
        //弹窗dismiss时调用
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
					//弹窗结束的收尾工作...
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.rl_close || v.getId()==R.id.tv_close ){
                dismiss();
        }
    }
}

コール:

 BottomPopWindow bottomDialog = new BottomPopWindow(this);
 bottomDialog.showAtLocation(findViewById(R.id.llParent), Gravity.BOTTOM, 0, 0);
公開された28元の記事 ウォンの賞賛1 ビュー519

おすすめ

転載: blog.csdn.net/qq_40575302/article/details/104706470