Android中实现Window弹窗

2023-01-12 18:58:22
import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

public class AlertWindow {

    public AlertWindow(Context context, String content, String btnStr, ActionCallback actionCallback) {
        initWindow(context,content,btnStr,actionCallback);
    }

    //Window
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams layoutParams;
    private Context mContext;
    private boolean addTag=false;//false表示view没有被add
    private View view;
    private EverScrollTextView everScrollTextView;
    private Button i_know;
    private LinearLayout out_view_lin;
    private void initWindow(Context context,String content,String btnStr, ActionCallback actionCallback){
        mContext=context;
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        layoutParams = new WindowManager.LayoutParams( 2024 /*WindowManager.LayoutParams.TYPE_PHONE*/);//TYPE_DISPLAY_OVERLAY
        layoutParams.gravity= Gravity.BOTTOM;
        layoutParams.format= PixelFormat.RGBA_8888;//设置透明
        layoutParams.x = 0;
        layoutParams.y = 0;

        layoutParams.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        /*layoutParams.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;*/

        view = LayoutInflater.from(context).inflate(R.layout.__448_alert_window, null);

        everScrollTextView=(EverScrollTextView) view.findViewById(R.id.alert_content);
        everScrollTextView.setText(content);

        i_know=(Button) view.findViewById(R.id.i_know);
        i_know.setText(btnStr);
        i_know.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                actionCallback.toDo(null);
                hide();
            }
        });

        out_view_lin=(LinearLayout) view.findViewById(R.id.out_view_lin);
        out_view_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hide();
            }
        });
    }

    private void hide(){
        BaseUtil.INSTANCE.runUi(()->{
            if (addTag){
                mWindowManager.removeView(view);
                addTag=false;
            }
            return null;
        });
    }

    public void show(){
        BaseUtil.INSTANCE.runUi(()->{
            if (!addTag){
                mWindowManager.addView(view,layoutParams);
                addTag=true;
            }
            return null;
        });
    }

}

  • 作者:绝命三郎
  • 原文链接:https://blog.csdn.net/qq_41008818/article/details/128262051
    更新时间:2023-01-12 18:58:22