当前位置: 首页 > article >正文

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进一步定制。

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.kler.cn/a/529557.html

相关文章:

  • w186格障碍诊断系统spring boot设计与实现
  • 《基于Scapy的综合性网络扫描与通信工具集解析》
  • 如何构建ObjC语言编译环境?构建无比简洁的clang编译ObjC环境?Windows搭建Swift语言编译环境?
  • IM 即时通讯系统-50-[特殊字符]cim(cross IM) 适用于开发者的分布式即时通讯系统
  • 【Python】第七弹---Python基础进阶:深入字典操作与文件处理技巧
  • 【贪心算法篇】:“贪心”之旅--算法练习题中的智慧与策略(一)
  • Clion开发STM32时使用stlink下载程序与Debug调试
  • MySQL基础学习总结(二)_select round(3
  • 【Rust自学】19.2. 高级trait:关联类型、默认泛型参数和运算符重载、完全限定语法、supertrait和newtype
  • MacBook Pro(M1芯片)Qt环境配置
  • 【工欲善其事】利用 DeepSeek 实现复杂 Git 操作:从原项目剥离出子版本树并同步到新的代码库中
  • 9 点结构模块(point.rs)
  • 面经——C语言——指针大小,内存分配,野指针,大小端
  • 【LeetCode: 598. 区间加法 II + 脑筋急转弯】
  • 我的Go+语言初体验——环境搭建并用命令行和 VScode 输出 “Hello World”_gop windows helloworld
  • 一些常用的HTML结构
  • 使用 EXISTS 解决 SQL 中 IN 查询数量过多的问题
  • C++ 哈希封装详解
  • E. Money Buys Happiness
  • UE5 蓝图计划 - Day 2-3:执行流与事件
  • 大模型能力评估数据集都有哪些?
  • 贪吃蛇实现
  • SpringBoot的配置(配置文件、加载顺序、配置原理)
  • UE5 蓝图学习计划 - Day 11:材质与特效
  • 大模型训练(5):Zero Redundancy Optimizer(ZeRO零冗余优化器)
  • 操作系统和中间件的信息收集