android弹窗之PopupWindow的简单使用

本文详细介绍如何在Android中使用PopupWindow,包括构造方法、常用API及代码示例。通过实例展示如何创建、显示和控制PopupWindow,以及如何处理触摸事件和设置动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

这个效果很简单,我们来看一下怎么去实现,Android为我们提供了一个PopupWindow类

一、PopupWindow的构造

  • public PopupWindow (Context context)
  • public PopupWindow(View contentView, int width, int height)
  • public PopupWindow(View contentView)
  • public PopupWindow(View contentView, int width, int height, boolean focusable)

二、常用方法

  • setContentView(View contentView):设置PopupWindow显示的View
  • getContentView():获得PopupWindow显示的View
  • showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
  • showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
  • showAtLocation(View parent, int gravity, int x, int y): 相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移 PS:parent这个参数只要是activity中的view就可以了!
  • setWidth/setHeight:设置宽高,也可以在构造方法那里指定好宽高, 除了可以写具体的值,还可以用WRAP_CONTENT或MATCH_PARENT, popupWindow的width和height属性直接和第一层View相对应。
  • setFocusable(true):设置焦点,PopupWindow弹出后,所有的触屏和物理按键都由PopupWindows 处理。其他任何事件的响应都必须发生在PopupWindow消失之后,(home 等系统层面的事件除外)。 比如这样一个PopupWindow出现的时候,按back键首先是让PopupWindow消失,第二次按才是退出 activity,准确的说是想退出activity你得首先让PopupWindow消失,因为不并是任何情况下按back PopupWindow都会消失,必须在PopupWindow设置了背景的情况下 。
  • setAnimationStyle(int):设置动画效果

三、写代码

java代码

public class Main7Activity extends AppCompatActivity {

    private Button btn_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main7);
        btn_show = findViewById(R.id.btn_show);
        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initPopupWindow(view);
            }
        });
    }

    private void initPopupWindow(View v) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_popip,null,false);
        TextView btn_xixi = view.findViewById(R.id.btn_xixi);
        TextView btn_hehe = view.findViewById(R.id.btn_hehe);
        //1.构造一个PopupWindow,参数依次是加载的View,宽高
        final PopupWindow popupWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                true);

//        popupWindow.setAnimationStyle(R.anim.anim_pop);//设置加载动画

        //这些为了点击非PopupWindow区域,PopupWindow会消失的,如果没有下面的
        //代码的话,你会发现,当你把PopupWindow显示出来了,无论你按多少次后退键
        //PopupWindow并不会关闭,而且退不出程序,加上下述代码可以解决这个问题
        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
            }
        });
//        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.popup_window));    //要为popWindow设置一个背景才有效

        //设置popupWindow显示的位置,参数依次是参照View,x轴的偏移量,y轴的偏移量
        popupWindow.showAsDropDown(v,v.getWidth()/4,  30);

        //设置popupWindow里的按钮的事件
        btn_xixi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Main7Activity.this, "你点击了嘻嘻~", Toast.LENGTH_SHORT).show();
            }
        });
        btn_hehe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Main7Activity.this, "你点击了呵呵~", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });
    }

}

布局文件

<?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:gravity="center"
    tools:context=".Main7Activity">

    <Button
        android:id="@+id/btn_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示PopupWindow"/>

</LinearLayout>

item_popip.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:background="#ffffff"
    android:orientation="vertical">

    <TextView
        android:id="@+id/btn_xixi"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:gravity="center"
        android:text="嘻嘻"
        android:textColor="#666"
        android:textSize="14sp" />

    <View
        android:layout_width="60dp"
        android:layout_height="0.5dp"
        android:layout_marginHorizontal="10dp"
        android:background="#ededed"/>

    <TextView
        android:id="@+id/btn_hehe"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="呵呵"
        android:gravity="center"
        android:textColor="#666"
        android:textSize="14sp" />

</LinearLayout>

anim_pop.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0.1" android:toAlpha="1.0"/>
</set>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值