PopUpWindow(基础篇)

PopUpWindow(基础篇)

为什么用PopupWindow

PopupWindow相较于AlertDialog,可以很方便的指定要显示的位置,如相对某个控件的位置,相对父容器的位置

PopupWindow构造函数

//方法一:
public PopupWindow (Context context)
//方法二:
public PopupWindow(View contentView)
//方法三:
public PopupWindow(View contentView, int width, int height)
//方法四:
public PopupWindow(View contentView, int width, int height, boolean focusable)

有四个构造函数,但要生成一个PopupWindow最基本的三个参数是一定要设置的:View contentView,int width, int height ;少任意一个都不能显示PopupWindow

PopupWindow显示函数

//相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor):
//相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;
showAsDropDown(View anchor, int xoff, int yoff):
//相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
showAtLocation(View parent, int gravity, int x, int y):

PopupWindow其它控制函数

//关闭PopupWindow
public void dismiss()
//设置PopupWindow获得焦点,获得焦点后可以交互
public void setFocusable(boolean focusable)
//设置PopupWindow是否可以触摸交互
public void setTouchable(boolean touchable)
//设置点击PopupWindow外的区域是否关闭
public void setOutsideTouchable(boolean touchable)
//设置了背景
public void setBackgroundDrawable(Drawable background)

作为基础篇,只显示一个最简单的PopupWindow,效果

在这里插入图片描述

看代码
MainActivity.java

public class MainActivity extends AppCompatActivity {
    PopupWindow popupWindow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_open_simple_popupwindow:
                openSimplePopUpWindow();
                break;
        }

    }

    private void openSimplePopUpWindow() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_simple_popupwindow,null);
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.showAtLocation(ll_main, Gravity.BOTTOM,0,0);
    }

    private void dismissPopupWindow(){
        if(popupWindow!=null && popupWindow.isShowing()){
            popupWindow.dismiss();
        }
    }

}

特别注意:要想实现点击 PopupWindow 窗口以外区域,关闭PopupWindow效果,必须加上下面代码

popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(true);

activity_main.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:id="@+id/ll_main"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_open_simple_popupwindow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开一个最简单PopUpWindow!"
        android:onClick="onClick"
        />
</LinearLayout>

PopupWindow 显示的布局文件layout_simple_popupwindow.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="@color/gray">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="我是一个PopUpWindow"
        android:textColor="@color/colorAccent"
        android:textSize="30sp"/>
</LinearLayout>

一个简单的PopupWindow就显示出来了。demo在下一篇,下一篇介绍了PopUpWindow(提高篇),涉及带列表选择的PopUpWindow和带动画效果的PopUpWindow。

下一篇 PopUpWindow(提高篇)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值