UI控件--PopWindow

本文详细介绍了如何在Android应用中使用PopWindow和AlertDialog,并通过示例代码展示了自定义布局和不同显示方法的具体实现。

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

  • PopWindow和AlertDialog的使用非常的相似
  • 效果图如下:
    这里写图片描述
  • 自定义布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView_exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:gravity="center"
        android:background="#004400"
        android:layout_gravity="center"
        android:textSize="40sp"
        android:text="退出"/>

    <TextView
        android:id="@+id/textView_cancal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:background="#990055"
        android:layout_gravity="center"
        android:textSize="40sp"
        android:gravity="center"
        android:text="取消"/>

</LinearLayout>
  • 代码如下
        if (!popupWindow.isShowing()){
            popupWindow = new PopupWindow();
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_popwindow, null);
            TextView textView_exit = (TextView) view.findViewById(R.id.textView_exit);
            TextView textView_cancal = (TextView) view.findViewById(R.id.textView_cancal);
            textView_cancal.setOnClickListener(this);
            textView_exit.setOnClickListener(this);

            popupWindow.setContentView(view);
            popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
            popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
//        View rootView=LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main,null);
//        popupWindow.showAtLocation(rootView,Gravity.BOTTOM,0,0);

            popupWindow.showAsDropDown(button,0,0);
           }
  • 可以通过判断popupWindow.isShowing()方法,来确定是否已经打开了popWindow,以避免popWindow的重复打开
  • 对自定义控件的点击事件的处理我并未上传,仅仅是一个吐司提示,读者请自行尝试。
  • 第一种显示popWindow的方法
popupWindow.showAsDropDown(button,0,0)
  • 方法表示popWindow放置在哪个控件的下方,以及x和y的偏移量。
  • 第二种显示popWindow的方法
View rootView=LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main,null);
popupWindow.showAtLocation(rootView,Gravity.BOTTOM,0,0);
  • 需要加载popWindow的所在的布局的实例,然后设置其父控件,对齐方式,以及x、y的偏移量
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值