• 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
  • 相关阅读:
    vuex状态管理(二)超级详细使用教程(包含辅助函数map的使用),一看就会,一学就懂
    专业创新实践报告--YOLO v3算法详解以及和Faster-RCNN的比较
    STM32 | 利用STM32CubeMX初始化一个STM32工程,图有点多,因为超详细(以STM32G431RBT6举例)
    注意力机制的qkv
    优选丨 5 大用例设计笔试大题,附超详细解析
    字符串Hash学习笔记
    老杨说运维 | 运维大数据价值探索
    python数据库——Mongodb
    ESP8266-Arduino编程实例-MAG3110磁力计驱动
    40度高温,如何通过SOLIDWORKS找到室内最凉快的地方?
  • 原文地址:https://blog.csdn.net/qq_15181569/article/details/127628125