android java系统弹窗的基础模板

news/2025/2/3 14:34:16 标签: android, java, 开发语言

1、资源文件

app\src\main\res\layout下增加custom_pop_layout.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);
    }
}


http://www.niftyadmin.cn/n/5840873.html

相关文章

redis集群理论详解

一. Redis集群发展历程 本片文章只介绍集群理论知识&#xff0c;不包含Redis集群搭建教程 教程文章请点击docker搭建redis集群&#xff08;三主三从&#xff09; 阶段一&#xff1a;单机版Redis 优点&#xff1a; 简单&#xff1a;易于部署和使用&#xff0c;适合小型项目或初期…

MacBook Pro(M1芯片)Qt环境配置

MacBook Pro&#xff08;M1芯片&#xff09;Qt环境配置 1、准备 试图写一个跨平台的桌面应用&#xff0c;此时想到了使用Qt&#xff0c;于是开始了搭建开发环境&#xff5e; 在M1芯片的电脑上安装&#xff0c;使用brew工具比较方便 Apple Silicon&#xff08;ARM/M1&#xf…

《苍穹外卖》项目学习记录-Day11订单统计

根据起始时间和结束时间&#xff0c;先把begin放入集合中用while循环当begin不等于end的时候&#xff0c;让begin加一天&#xff0c;这样就把这个区间内的时间放到List集合。 查询每天的订单总数也就是查询的时间段是大于当天的开始时间&#xff08;0点0分0秒&#xff09;小于…

从零开始:用Qt开发一个功能强大的文本编辑器——WPS项目全解析

文章目录 引言项目功能介绍1. **文件操作**2. **文本编辑功能**3. **撤销与重做**4. **剪切、复制与粘贴**5. **文本查找与替换**6. **打印功能**7. **打印预览**8. **设置字体颜色**9. **设置字号**10. **设置字体**11. **左对齐**12. **右对齐**13. **居中对齐**14. **两侧对…

IELTS口语练习题库

IELTS口语1-4月题库 Part 1 Gifts Have you ever sent handmade gifts to others? Yes, I have. I once made a scrapbook for my best friend’s birthday. It included photos of our memories together and some handwritten notes. She loved it because it was personal…

Swoole如何实现多进程

Swoole 是一个高性能的 PHP 扩展&#xff0c;它提供了多进程模型来解决传统 PHP 环境中的并发处理问题。以下是 Swoole 实现多进程的具体方式和原理&#xff1a; 一、多进程模型的实现 进程创建与管理&#xff1a; Swoole 提供了 Swoole\Process 类&#xff08;或在旧版本中使…

abc 390 D(暴搜 复杂度用 bell数 证明 n 的集合的划分方法的数目)

D题意&#xff1a; 将 长度为 N 的数组 划分为集合 有多少种不同的 异或和 这道题做出来和bell 数没什么关系&#xff0c;如果要证明复杂度那么就需要bell 数 #include <bits/stdc.h> using namespace std; typedef pair<int, int> PII; #define int long long i…

无需破解版这个永久免费

聊一聊 有人问我有没有可以把视频或音频人的声音和音乐分离出来的软件。 这个当然有。 只要你能想出来的&#xff0c;基本都有工具能实现。 就怕你想不出来。 有时候能想出来&#xff0c;但不知道怎么表达。 所以&#xff0c;关注我&#xff0c;给大家分享实用、有趣的软…