您的位置:首页 > 移动开发 > Android开发

android中popupwindow详解

2018-01-11 14:42 246 查看

方法一:

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息