android中popupwindow详解

Snow_Ice_Yang · · 167 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

方法一:

将显示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);
    }
效果图如下:


本文来自:CSDN博客

感谢作者:Snow_Ice_Yang

查看原文:android中popupwindow详解

167 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet