android java系统弹窗的基础模板

1、资源文件

app\src\main\res\layout下增加custom_pop_layout.xml

定义弹窗的控件资源。

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

	<ImageView
		android:id="@+id/customPopView"
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:background="#ff000000" />

    <Button
        android:id="@+id/exampleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:layout_marginStart="40dp"
        android:text="示例按钮"
        app:layout_constraintStart_toStartOf="@id/customPopView"
        app:layout_constraintTop_toTopOf="@id/customPopView" />
</androidx.constraintlayout.widget.ConstraintLayout>

2、java代码

CustomPopUtil初始化有下面几点:

1)获取资源文件的rootView,添加到系统管理器下,达到系统级弹窗效果,可在其他app上弹出。

2)params = new WindowManager.LayoutParams是用来设置弹窗的参数,包括大小、坐标、透明度等。后面可根据需要修改。

3)rootView.setVisibility(View.GONE)表示初始化隐藏。

4)需要弹出时,调用接口show(),如果弹出时,想要修改弹窗的界面参数,可在show接口里调用WindowManager.LayoutParams进一步定制。

java 复制代码
import static android.content.Context.WINDOW_SERVICE;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.graphics.PixelFormat;
import android.widget.Button;

public class CustomPopUtil {
    private View rootView;
    private Button exampleButton;
    // 可增加其他ui控件

    @SuppressLint("InflateParams")
    public void init(Context context) {
        rootView = LayoutInflater.from(context).inflate(R.layout.custom_pop_layout, null);

        WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);

        WindowManager.LayoutParams params = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.OPAQUE);
        }
        windowManager.addView(rootView, params);

        rootView.setVisibility(View.GONE);

        exampleButton = rootView.findViewById(R.id.exampleButton);
        exampleButton.setOnClickListener(v -> {
            // do some logic
        });
    }

    public void show() {
        rootView.setVisibility(View.VISIBLE);
    }
    public void hide() {
        rootView.setVisibility(View.GONE);
    }
}
相关推荐
南玖yy1 分钟前
内存安全革命:工具、AI 与政策驱动的 C 语言转型之路
c语言·开发语言·c++·人工智能·安全·c++23·c++基础语法
Lu Yao_14 分钟前
golang -- 如何获取变量类型
android·java·golang
yuanpan15 分钟前
平面坐标系中判断点P是否在线段上AB上的常用方法总结
开发语言·python·平面·点线关系
二十雨辰18 分钟前
[Spring]-认识Spring
java·数据库·spring
海拥✘19 分钟前
用Python监控金价并实现自动提醒!附完整源码
开发语言·python
eguid_122 分钟前
WebRTC流媒体传输协议RTP点到点传输协议介绍,WebRTC为什么使用RTP协议传输音视频流?
java·网络协议·音视频·webrtc·实时音视频
码农娟23 分钟前
根据文件路径获取base64照片
java
手机忘记时间29 分钟前
在R语言中如何将列的名字改成别的
java·前端·python
苹果酱056730 分钟前
[数据库之十一] 数据库索引之联合索引
java·vue.js·spring boot·mysql·课程设计
zhojiew36 分钟前
istio in action之流量控制与路由
java·数据库·istio