android 简单使用PopupWindow

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_41659081/article/details/102624327

简单封装的一个popupWindow工具类

查了网上的一堆资料之后,自己简单封装的一个工具类,我这里只是放了两个简单的按钮,想要更多的选项或者样式可以自己修改布局文件

public class PopupWindowUtils {

    private static PopupWindow mPopWindow;

    public static void showPopupWindow(Activity activity, int resources, final OnClickExitOrCancelListener onClickExitOrCancelListener){
        View contentView = LayoutInflater.from(activity).inflate(R.layout.popup_bottom_exit,null);
        mPopWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,true);
        LinearLayout popup_exit = contentView.findViewById(R.id.popup_exit);
        popup_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickExitOrCancelListener.clickExit(mPopWindow);
            }
        });
        LinearLayout popup_cancel = contentView.findViewById(R.id.popup_cancel);
        popup_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickExitOrCancelListener.clickCancel(mPopWindow);
            }
        });
        //显示PopupWindow
        //这里的resources,由外部传入,是决定popupwindow在哪个布局跳出来
        View rootView = LayoutInflater.from(activity).inflate(resources,null);
        mPopWindow.setAnimationStyle(R.style.popup_window_animation);
        //外部是否可以点击
        mPopWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopWindow.setOutsideTouchable(true);
		
        mPopWindow.showAtLocation(rootView, Gravity.BOTTOM,0,0);
    }

    public interface OnClickExitOrCancelListener{
        void clickExit(PopupWindow popupWindow);
        void clickCancel(PopupWindow popupWindow);
    }


}

布局文件popup_bottom_exit

要想修改跳出的popupWindow样式,修改这个布局文件,然后再去java代码部分修改绑定控件,和要对控件做出的操作

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_white_round_corner"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/popup_exit"
            android:layout_width="match_parent"
            android:layout_height="50dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="退出登录"
                android:textSize="16sp"
                android:textColor="#C92727"
                android:gravity="center"/>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:background="#ededed"/>

        <LinearLayout
            android:id="@+id/popup_cancel"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginBottom="20dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="取消"
                android:textSize="16sp"
                android:textColor="@android:color/black"
                android:gravity="center"/>
        </LinearLayout>


    </LinearLayout>

</RelativeLayout>

样式:popup_window_animation

<style name="popup_window_animation" parent="@android:style/Animation.Activity">
        <item name="android:windowEnterAnimation">@anim/anim_popup_enter</item>
        <item name="android:windowExitAnimation">@anim/anim_popup_exit</item>
    </style>

两个动画文件:anim_popup_enter和anim_popup_exit

anim_popup_enter:

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

    <translate
        android:duration="800"
        android:fromXDelta="0"
        android:fromYDelta="100%p"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="0"
        android:toYDelta="0"/>

</set>

anim_popup_exit:

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

    <translate
        android:duration="800"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="0"
        android:toYDelta="100%p" />

</set>

猜你喜欢

转载自blog.csdn.net/qq_41659081/article/details/102624327
今日推荐