控件PopupWindow

Posted 一只小阿大嗷

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了控件PopupWindow相关的知识,希望对你有一定的参考价值。

常用方法

1.setContentView(View contentView) 设置PopupWindow显示view
2.showAsDropDown(View anchor) 相对某个控件的位置(正左下方),无偏移
3.showAsDropDown(View anchor,int xoff,int yoff) 相对某个控件的位置,有偏移
4.setFocusable(boolean focusable) 设置是否获取焦点
5.setBackgroundDrawable(Drawable background) 设置背景
6.dismiss() 关闭弹窗
7.setAnimationStyle(int animationStyle) 设置加载动画
8.setTouchable(boolean touchable) 设置触摸使能
9.setOutsideTouchable(boolean touchable) 设置PopupWindow外面的触摸使能

注意
public PopupWindow(View contentView, int width, int height, boolean focusable)

PopupWindow方法最后一个参数focusable和setFocusable用处是一样的。作用都是点击PopupWindow外的时候,收缩弹窗

代码

MainActivity.java

package com.example.popupwindow;

import androidx.appcompat.app.AppCompatActivity;

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 = "Finny";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void FinnyClick(View view) {
        View popupView = getLayoutInflater().inflate(R.layout.popup_view, null);

        Button btn1 = popupView.findViewById(R.id.btn_1);
        Button btn2 = popupView.findViewById(R.id.btn_2);

        PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,true);

        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.tp));

        popupWindow.showAsDropDown(view,view.getWidth(),-view.getHeight());

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "onClick: 住在上海" );
                popupWindow.dismiss();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "onClick: 住在北京" );
                popupWindow.dismiss();
            }
        });
    }
}

activity_main.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">

    <Button
        android:text="弹出可选城市"
        android:onClick="FinnyClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

popup_view.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"
    android:background="@mipmap/ic_launcher">

    <Button
        android:id="@+id/btn_1"
        android:text="上海"
        android:padding="5dp"
        android:textSize="18dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btn_2"
        android:text="北京"
        android:padding="5dp"
        android:textSize="18dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
点击了上海和北京后会loge输出
每点一个按钮,都会dismiss()
点击弹窗外的,会自动收缩

以上是关于控件PopupWindow的主要内容,如果未能解决你的问题,请参考以下文章

控件PopupWindow

Android基础控件——PopupWindow模仿ios底部弹窗

Android 多个控件绑定同一个PopupWindow,当点击popupWindow中的控件时获取到触发popWindow控件的文本

android中PopupWindow弹出窗体后,为啥不能点击其他控件

Android开发,如何让PopupWindow弹出时外部控件不可点击?

使弹出popupwindow界面外部的控件不能点击