android中popupwindow详解

方法一:

将显示popupwindow包装成方法:

private void showPopupwindow() {
        View v = LayoutInflater.from(HelpAndFeedbackActivity.this).inflate(R.layout.popupwindow_submit_feedback, null);
        final PopupWindow submitFeedbackPopupwindow = new PopupWindow(v, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
        View rootView = LayoutInflater.from(this).inflate(R.layout.activity_help_and_feedback, null);
        //设置SelectPicPopupWindow弹出窗体可点击
        submitFeedbackPopupwindow.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        submitFeedbackPopupwindow.setAnimationStyle(R.style.PopupwindowDialogAnimation);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        submitFeedbackPopupwindow.setBackgroundDrawable(dw);
        submitFeedbackPopupwindow.showAtLocation(rootView, Gravity.CENTER, 0, 0);
        TextView tv_title = (TextView) v.findViewById(R.id.tv_addFloder);
        tv_title.setText("提交反馈");
        TextView iv_yes = (TextView) v.findViewById(R.id.iv_sure);
        //popupwindow的确定按钮的点击事件
        iv_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(HelpAndFeedbackActivity.this,"提交反馈",Toast.LENGTH_SHORT).show();
                submitFeedbackPopupwindow.dismiss();
            }
        });
        v.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int top = v.findViewById(R.id.ll_help_submit).getTop();
                int bottom = v.findViewById(R.id.ll_help_submit).getBottom();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < top || y > bottom) {
                        submitFeedbackPopupwindow.dismiss();
                    }
                }
                return true;
            }
        });
    }
其中popupwindow的布局popupwindow_submit_feedback.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">
    <LinearLayout
        android:id="@+id/ll_help_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp15"
        android:layout_marginRight="@dimen/dp15"
        android:layout_marginTop="@dimen/dp100"
        android:background="@color/white"
        android:orientation="vertical">

        <include
            layout="@layout/title_popupwindow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/et_newname"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp180"
            android:paddingRight="@dimen/dp15"
            android:background="@null"
            android:gravity="top"
            android:layout_marginTop="10dp"
            android:hint="我的机器为什么..."
            android:paddingLeft="@dimen/dp15"
            android:textSize="16sp" />
    </LinearLayout>
</LinearLayout>
效果图如下:

方法二:

将popupwindow封装成类:

public class ExitAppPopupwindow extends PopupWindow {
    private TextView yesExit;
    private TextView cancelExit;

    public ExitAppPopupwindow(Context context, View.OnClickListener onClickListener) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View menuView = inflater.inflate(R.layout.popupwindow_exit, null);
        yesExit = (TextView) menuView.findViewById(R.id.tv_exitpopup_yesexit);
        cancelExit = (TextView) menuView.findViewById(R.id.tv_exitpopup_cancel);
        yesExit.setOnClickListener(onClickListener);
        cancelExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        //设置SelectPicPopupWindow的View
        this.setContentView(menuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopupwindowDialogAnimation);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);

        menuView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int top = menuView.findViewById(R.id.ll_exitpopup).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < top) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }
}
其中popupwindow_exit.xml布局如下:

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

    <LinearLayout
        android:id="@+id/ll_exitpopup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="@dimen/dp10"
        android:layout_marginRight="@dimen/dp10"
        android:background="@color/gray"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:gravity="center"
            android:padding="@dimen/dp10"
            android:text="确认要退出此账号吗?" />

        <TextView
            android:id="@+id/tv_exitpopup_yesexit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp1"
            android:background="@color/white"
            android:gravity="center"
            android:padding="@dimen/dp10"
            android:text="确认退出"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/tv_exitpopup_cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp1"
            android:background="@color/white"
            android:gravity="center"
            android:padding="@dimen/dp10"
            android:textColor="@color/black"
            android:text="取消" />
    </LinearLayout>


</RelativeLayout>
在主方法中调用ExitAppPopupwindow:

private void exitApp() {
        exitPopupWindow = new ExitAppPopupwindow(this, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.tv_exitpopup_yesexit:
                        exitPopupWindow.dismiss();
                        mainPresenter.logout();
                        SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
                        sp.edit().putString("userpwd", "").commit();
                        finish();
                        break;
                    case R.id.tv_exitpopup_cancel:
                        exitPopupWindow.dismiss();
                        break;
                    default:
                        break;
                }
            }
        });
        exitPopupWindow.showAtLocation(this.findViewById(R.id.drawer_layout), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }
效果图如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值