Android 中 PopupWindow 的基本使用

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>

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

猜你喜欢

转载自blog.csdn.net/YKenan/article/details/113313233