• Android开发PopupWindow的使用


    一、效果展示

    在这里插入图片描述

    二、源码

    MainActivity.java

    package com.example.learning01;
    
    import androidx.appcompat.app.AlertDialog;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.appcompat.widget.Toolbar;
    
    import android.annotation.SuppressLint;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.PopupWindow;
    
    public class MainActivity extends AppCompatActivity {
    
        private static final String TAG ="MainActivity";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
        }
    
        public void popwindow_show(View view) {
            View popwindowView = getLayoutInflater().inflate(R.layout.activity_popwindow, null);
    
            PopupWindow popupWindow = new PopupWindow(popwindowView,ViewGroup.MarginLayoutParams.WRAP_CONTENT,ViewGroup.MarginLayoutParams.WRAP_CONTENT,true);
            popupWindow.showAsDropDown(view);
    
    
            Button btn1 = popwindowView.findViewById(R.id.btn1);
            Button btn2 = popwindowView.findViewById(R.id.btn2);
    
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popupWindow.dismiss();
                }
            });
            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popupWindow.dismiss();
                }
            });
    
    
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    activity_main.xml

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity"
        android:padding="10dp">
    
        <Button
            android:layout_gravity="right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="popwindow"
            android:onClick="popwindow_show"/>
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    activity_popwindow.xml

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/test">
    
        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="btn1"/>
        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="btn2"/>
    
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    Linux red 安装多个版本mysql
    Android通用方法获取mac地址和以太网信息ip地址、网关、dns等
    江同志是怎样逆划水的?
    Gradle 配置国内镜像
    Reptile_urllib的基本使用
    04_瑞萨GUI(LVGL)移植实战教程之驱动LCD屏(SPI)
    嵌入式快速入门学习笔记-input输入子系统
    如图是怎样实现点动和连续的
    2022.03 Kali Linux Maltego工具
    【Mysql】EXPLAIN
  • 原文地址:https://blog.csdn.net/qq_15181569/article/details/127628125