相对应对话空,我觉得popupwindow更具有优势.(个人观点哈哈)
一:这里我对弹出pop做了简单的两个封装,显示一个按钮的,和显示两个按钮的
1,显示一个按钮的pop,的XML文件先贴效果图
这个是一个按钮的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:background="@drawable/btn_white_30"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/tv_one_bt_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding_10"
android:gravity="center"
android:text="请先对订单进行评价"
android:layout_marginTop="@dimen/padding_20"
android:layout_marginBottom="@dimen/padding_30"
style="@style/Font_Middle_Gray" />
</LinearLayout>
<View style="@style/Style_hLine" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_one_bt_ok"
android:layout_width="@dimen/padding_0"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="确定"
android:gravity="center"
android:padding="@dimen/padding_10"
style="@style/Font_Middle_Blue" />
</LinearLayout>
</LinearLayout>
2,现在在写一个按钮的pop,以后使用的时候直接new我这个对象就可以了,
package com.lzyc.ybtappcal.widget.popupwindow;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.lzyc.ybtappcal.R;
import com.lzyc.ybtappcal.util.DensityUtils;
import com.lzyc.ybtappcal.util.ScreenUtils;
import com.lzyc.ybtappcal.util.Util;
/**
* 备注:popupwindow
*/
public class PopupWindowOneButton extends PopupWindow {
private Activity mContext;
private TextView tv_content;
private TextView tv_ok;
public TextView getTv_content() {
return tv_content;
}
public void setTv_content(TextView tv_content) {
this.tv_content = tv_content;
}
public TextView getTv_ok() {
return tv_ok;
}
public void setTv_ok(TextView tv_ok) {
this.tv_ok = tv_ok;
}
public PopupWindowOneButton(Activity context) {
this.mContext = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View conentView = inflater.inflate(R.layout.popup_one_button, null);
tv_content = (TextView) conentView.findViewById(R.id.tv_one_bt_content);
tv_ok = (TextView) conentView.findViewById(R.id.tv_one_bt_ok);
int h = context.getWindowManager().getDefaultDisplay().getHeight();
int w = ScreenUtils.getScreenWidth() - DensityUtils.dp2px(60); //ScreenUtils DensityUtils 获取屏幕和转换dp的工具类
// 设置SelectPicPopupWindow的View
this.setContentView(conentView);
// 设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(w);
// 设置SelectPicPopupWindow弹出窗体的高
this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
// 设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
this.setOutsideTouchable(true);
// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
this.setBackgroundDrawable(new BitmapDrawable());
// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
// 设置SelectPicPopupWindow弹出窗体动画效果
}
/**
* 显示popupWindow
*
* @param parent
*/
public void showPopupWindow(View parent, int gravity) {
if (!this.isShowing()) {
// 以下拉方式显示popupwindow
Util.setBackgroundAlpha(mContext, 0.5f);
this.showAtLocation(parent, gravity, 0, 0);
} else {
this.dismiss();
}
}
@Override
public void dismiss() {
super.dismiss();
Util.setBackgroundAlpha(mContext, 1f);
}
}
2,显示两个按钮的pop,就像对话框一样,看效果图
1,先贴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:background="@drawable/btn_white_30"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/tv_two_bt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示"
android:layout_marginTop="@dimen/padding_10"
style="@style/Font_Big_Black" />
<TextView
android:id="@+id/tv_two_bt_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding_10"
android:gravity="center"
android:text="请先对订单进行评价"
android:layout_marginTop="@dimen/padding_20"
android:layout_marginBottom="@dimen/padding_30"
style="@style/Font_Middle_Gray" />
</LinearLayout>
<View style="@style/Style_hLine" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_two_bt_cancel"
android:layout_width="@dimen/padding_0"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="取消"
android:gravity="center"
android:padding="@dimen/padding_10"
style="@style/Font_Middle_Gray" />
<View style="@style/Style_vLine" />
<TextView
android:id="@+id/tv_two_bt_ok"
android:layout_width="@dimen/padding_0"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="确认"
android:gravity="center"
android:padding="@dimen/padding_10"
style="@style/Font_Middle_Blue" />
</LinearLayout>
</LinearLayout>
2,再贴显示pop的代码,之后使用直接new该类对象就可以了
package com.lzyc.ybtappcal.widget.popupwindow;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.lzyc.ybtappcal.R;
import com.lzyc.ybtappcal.util.DensityUtils;
import com.lzyc.ybtappcal.util.ScreenUtils;
import com.lzyc.ybtappcal.util.Util;
/**
*
* 备注:towpopupwindow
*/
public class PopupWindowTwoButton extends PopupWindow {
private Activity mContext;
private TextView tv_title;
private TextView tv_content;
private TextView tv_cancel;
private TextView tv_ok;
public TextView getTv_title() {
return tv_title;
}
public void setTv_title(TextView tv_title) {
this.tv_title = tv_title;
}
public TextView getTv_content() {
return tv_content;
}
public void setTv_content(TextView tv_content) {
this.tv_content = tv_content;
}
public TextView getTv_cancel() {
return tv_cancel;
}
public void setTv_cancel(TextView tv_cancel) {
this.tv_cancel = tv_cancel;
}
public TextView getTv_ok() {
return tv_ok;
}
public void setTv_ok(TextView tv_ok) {
this.tv_ok = tv_ok;
}
public PopupWindowTwoButton(Activity context) {
this.mContext = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View conentView = inflater.inflate(R.layout.popup_two_button, null);
tv_title = (TextView) conentView.findViewById(R.id.tv_two_bt_title);
tv_content = (TextView) conentView.findViewById(R.id.tv_two_bt_content);
tv_cancel = (TextView) conentView.findViewById(R.id.tv_two_bt_cancel);
tv_ok = (TextView) conentView.findViewById(R.id.tv_two_bt_ok);
int w = ScreenUtils.getScreenWidth() - DensityUtils.dp2px(60);
int h = context.getWindowManager().getDefaultDisplay().getHeight();
// 设置SelectPicPopupWindow的View
this.setContentView(conentView);
// 设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(w);
// 设置SelectPicPopupWindow弹出窗体的高
this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
// 设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
this.setOutsideTouchable(true);
// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
this.setBackgroundDrawable(new BitmapDrawable());
// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
// 设置SelectPicPopupWindow弹出窗体动画效果
}
/**
* 显示popupWindow
*
* @param parent
*/
public void showPopupWindow(View parent, int gravity) {
if (!this.isShowing()) {
// 以下拉方式显示popupwindow
Util.setBackgroundAlpha(mContext, 0.5f);
this.showAtLocation(parent, gravity, 0, 0);
} else {
this.dismiss();
}
}
@Override
public void dismiss() {
super.dismiss();
Util.setBackgroundAlpha(mContext, 1f);
}
}
//简单使用举例:
*申明一下,这里我的需求是取消和确定对调一下,使用这个的监听我写反了
private void popupClearHistory() {
final PopupWindowTwoButton twoButton = new PopupWindowTwoButton(this);
twoButton.getTv_ok().setVisibility(View.VISIBLE);
twoButton.getTv_title().setText("清空");
twoButton.getTv_content().setText("你确定要清空历史记录吗?");
twoButton.getTv_cancel().setText("确定");
twoButton.getTv_cancel().setTextColor(Color.parseColor("#3E6398"));
twoButton.getTv_ok().setText("取消");
twoButton.getTv_ok().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
twoButton.dismiss();
return;
}
});
twoButton.getTv_cancel().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//确定的逻辑处理(本来是取消直接dismiss就可以了,但是我这里需求反了,所以在这里写确定的逻辑)
twoButton.dismiss();
}
});
twoButton.showPopupWindow(historyLinear, Gravity.CENTER);
}
二:除了上面常见的使用之外,还有很多的地方使用到pop这里我就贴一个BasePopupwindow,之后使用到直接new该类的对象就可以了
package com.lzyc.ybtappcal.widget.popupwindow;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.PaintDrawable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;
import com.lzyc.ybtappcal.R;
import com.lzyc.ybtappcal.util.LogUtil;
import com.lzyc.ybtappcal.util.ToastUtil;
import com.lzyc.ybtappcal.util.Util;
import com.sina.weibo.sdk.api.share.Base;
import com.umeng.message.proguard.O;
/**
* BasePopupWindow
* 作者:
* 备注BasePopupwindow
*/
public class BasePopupWindow extends PopupWindow {
private Activity mContext;
private View conentView;
public View getConentView() {
return conentView;
}
public void setConentView(View conentView) {
this.conentView = conentView;
}
public BasePopupWindow(Activity context, int itemLayoutId, int w, int h) {
this.mContext = context;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
conentView = inflater.inflate(itemLayoutId, null);
// int h = context.getWindowManager().getDefaultDisplay().getHeight();
// int w = context.getWindowManager().getDefaultDisplay().getWidth();
// 设置SelectPicPopupWindow的View
this.setContentView(conentView);
// 设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(w);
// 设置SelectPicPopupWindow弹出窗体的高
this.setHeight(h);
// 设置SelectPicPopupWindow弹出窗体可点击
this.setOutsideTouchable(false);
this.setFocusable(true);
this.setBackgroundDrawable(new BitmapDrawable());
this.setTouchable(true);
this.update();
// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
// 设置SelectPicPopupWindow弹出窗体动画效果
}
/**
* 显示popupWindow
*
* @param parent
*/
public void showPopupWindow(View parent, int gravity) {
if (!this.isShowing()) {
// 以下拉方式显示popupwindow
Util.setBackgroundAlpha(mContext, 0.5f);
setAnimationStyle(R.style.popup_anim_style);
this.showAtLocation(parent, gravity, 0, 0);
} else {
this.dismiss();
}
}
/**
* 显示popupWindow
*
* @param parent
*/
public void showDropDownPopupWindow(View parent, int x, int y) {
if (!this.isShowing()) {
// 以下拉方式显示popupwindow
Util.setBackgroundAlpha(mContext, 0.5f);
setAnimationStyle(R.style.popup_anim_style);
this.showAsDropDown(parent, x, y);
} else {
this.dismiss();
}
}
/**
* 右侧下拉popupWindow
*
* @param parent
*/
public void showDropPopRight(View parent, int gravity) {
if (!this.isShowing()) {
// 以下拉方式显示popupwindow
Util.setBackgroundAlpha(mContext, 0.5f);//设置添加屏幕的背景透明度
setAnimationStyle(R.style.popup_anim_style_bootom_right);
this.showAtLocation(parent, gravity, 0, 0);
} else {
this.dismiss();
}
}
/**
* 左侧下拉popupWindow
*
* @param parent
*/
public void showDropPopLeft(View parent, int gravity) {
if (!this.isShowing()) {
// 以下拉方式显示popupwindow
Util.setBackgroundAlpha(mContext, 0.5f);
setAnimationStyle(R.style.popup_anim_style_bootom_left);
this.showAtLocation(parent, gravity, 0, 0);
} else {
this.dismiss();
}
}
@Override
public void dismiss() {
super.dismiss();
Util.setBackgroundAlpha(mContext, 1f);
}
}
//BasePopupwindow中使用到设置背景透明度的方法
/**
* 设置添加屏幕的背景透明度
*
* @param bgAlpha
*/
public static void setBackgroundAlpha(Activity activity, float bgAlpha) {
Window window = activity.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
window.setAttributes(lp);
}