Android 中 PopupWindow 的基本使用

本文介绍了在Android中如何使用PopupWindow,展示了一个基本的PopupWindow实现,包括创建、设置属性以及显示和关闭PopupWindow的操作。示例代码展示了在按钮点击事件中弹出包含多个选项的PopupWindow,并通过点击事件处理各个选项。
摘要由CSDN通过智能技术生成

Android 中 PopupWindow 的基本使用

PopupWindowActivity 文件

package com.example.hello;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.example.hello.util.ToastUtil;

public class PopupWindowActivity extends AppCompatActivity {

    // 声明
    private Button btnPop;
    private PopupWindow pop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_window);
        // 获取
        btnPop = findViewById(R.id.btn_pop);
        btnPop.setOnClickListener(view -> {
            @SuppressLint("InflateParams") View inflate = getLayoutInflater().inflate(R.layout.layout_pop, null);
            // 实例化设置宽高
            pop = new PopupWindow(inflate, btnPop.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
            // 设置取消 (新版本设置 setFocusable 就可以不用设置这个属性)
            pop.setOutsideTouchable(true);
            // 设置点击切换
            pop.setFocusable(true);
            pop.showAsDropDown(btnPop);
        });

    }

    /**
     * 好的点击事件
     *
     * @param view view
     */
    public void showGood(View view) {
        pop.dismiss();
        ToastUtil.showShortToast(PopupWindowActivity.this, "Good");
    }
}

activity_popup_window.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:padding="15dp"
    tools:context=".PopupWindowActivity">

    <Button
        android:id="@+id/btn_pop"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/pop" />

</LinearLayout>

layout_pop.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/pop_tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white_up"
        android:gravity="center"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:text="@string/good"
        android:onClick="showGood"
        android:textColor="@color/black"
        android:textSize="23sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/black_low" />

    <TextView
        android:id="@+id/pop_tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white_up"
        android:gravity="center"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:text="@string/NotBad"
        android:textColor="@color/black"
        android:textSize="23sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/black_low" />

    <TextView
        android:id="@+id/pop_tv3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white_up"
        android:gravity="center"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:text="@string/bad"
        android:textColor="@color/black"
        android:textSize="23sp" />


</LinearLayout>

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值