Android PopupWindow类

1. 创建弹窗

PopupWindow就是弹出窗口的意思。

private PopupWindow createPopupWindow() {
    PopupWindow popupWindow = new PopupWindow(this);
    // 设置宽度
    popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    // 设置高度
    popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    // 设置背景
    popupWindow.setBackgroundDrawable(new ColorDrawable(
            getResources().getColor(R.color.colorProduct)));
    View view = getLayoutInflater().inflate(R.layout.popup_window_picture, null, false);
    view.findViewById(R.id.tv_take_photo).setOnClickListener(this);
    view.findViewById(R.id.tv_take_picture).setOnClickListener(this);
    view.findViewById(R.id.tv_cancel).setOnClickListener(this);
    // 设置界面
    popupWindow.setContentView(view);
    // true时界面可点
    popupWindow.setTouchable(true);
    // true时PopupWindow处理返回键
    popupWindow.setFocusable(true);
    // true时点击外部消失,如果touchable为false时,点击界面也消失
    popupWindow.setOutsideTouchable(true);

    // dismiss监听器
    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            LogTool.logi(LOG_TAG, "onDismiss");
        }
    });
    return popupWindow;
}

布局文件popup_window_picture.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="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:orientation="vertical"
        android:background="@drawable/pw_picture_bg" >
        <TextView
            android:id="@+id/tv_take_photo"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:gravity="center"
            android:text="拍照"
            android:textSize="15sp"
            android:textColor="#ff0a7ffb"/>

        <TextView
            android:id="@+id/tv_take_picture"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginTop="1dp"
            android:gravity="center"
            android:text="从相册选择"
            android:textSize="15sp"
            android:textColor="#ff0a7ffb"/>
    </LinearLayout>

    <TextView
        android:id="@+id/tv_cancel"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:gravity="center"
        android:text="取消"
        android:textSize="15sp"
        android:textColor="#ff0a7ffb"
        android:background="@drawable/pw_picture_bg" />

</LinearLayout>

背景pw_picture_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle" >

    <solid android:color="@color/white" />
    <corners android:radius="2dp" />

</shape>

主要方法

setWidth(int width) // 设置宽度
setHeight(int height) // 设置高度
setBackgroundDrawable(Drawable background) // 设置背景
setContentView(View contentView) // 设置界面
setTouchable(boolean touchable) // true时界面可点
setFocusable(boolean focusable) // true时PopupWindow处理返回键
setOutsideTouchable(boolean touchable) // true时点击外部消失,如果touchable为false时,点击界面也消失

2. 显示位置

  • 指定显示在Window的位置
    // 获取parent.getWindowToken()
    showAtLocation(View parent, int gravity, int x, int y)
    
    gravity指定方位,xy是偏移量。一般显示界面底部。
    popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
    
    效果如下
    在这里插入图片描述
  • View为锚点显示
    showAsDropDown(View anchor)
    showAsDropDown(View anchor, int xoff, int yoff)
    showAsDropDown(View anchor, int xoff, int yoff, int gravity)
    
    anchor是显示的锚点,默认是左下方。
    popupWindow.showAsDropDown(v);
    
    效果如下
    在这里插入图片描述

3. 设置弹窗显示效果

setAnimationStyle(int animationStyle)设置弹窗显示效果。animationStyle包含进入动画和退出动画。

styles.xml文件中定义animationStyle

<style name="popup_window_animation_style">
    <item name="android:windowEnterAnimation">@anim/anim_enter_from_bottom</item>
    <item name="android:windowExitAnimation">@anim/anim_exit_to_bottom</item>
</style>

进入动画anim_enter_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="100%"
    android:toYDelta="0"
    android:duration="1000" />

退出动画anim_exit_to_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="0"
    android:toYDelta="100%" />

最后调用setAnimationStyle(int)方法设置

popupWindow.setAnimationStyle(R.style.popup_window_animation_style);

效果如下
在这里插入图片描述

4. 设置背景透明度

设置背景透明度可以突出弹窗效果

private void backgroundAlpha(float bgAlpha) {
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.alpha = bgAlpha; //0.0-1.0
    getWindow().setAttributes(lp);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}

效果如下
在这里插入图片描述

相关文章
Android Toast类
Android PopupWindow类
Android AlertDialog类

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值