您的位置:首页 > 运维架构

PopupWindow的使用

2016-07-06 09:25 369 查看
首先创建popupWindow对象

popupWindow mpop =   new PopupWindow(contentView, width, height);

参数一表示要显示的布局,参数二表示popupWindow要显示的宽高;

为了响应返回键和界面外的其他界面

private ColorDrawable dw = new ColorDrawable(-00000);// popup的背景

mPopup.setBackgroundDrawable(dw);//这个是自己创建的背景,否则当点击以外关闭popupWindow

mPopup.showAsDropDown(anchor, xoff, yoff);

参数一表示要关联的控件,参数二和参数三表示位置

如果想关闭对话框用dismiss();

底部弹出的PopupWindow的实现

第一步:设计弹出窗口xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">

<LinearLayout
android:id="@+id/firmOrder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/white"
android:gravity="bottom"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center_vertical"
android:orientation="horizontal">

<ImageView
android:layout_width="45dp"
android:layout_height="match_parent"
android:scaleType="center"
android:src="@mipmap/weixin_pay"/>

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Wechat"
android:textColor="@color/black"
android:textSize="14sp"/>

<ImageView
android:id="@+id/Wechat_click"
android:layout_width="45dp"
android:layout_height="match_parent"
android:scaleType="center"
android:src="@drawable/circle_select_bg"/>
</LinearLayout>

<View style="@style/myline"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center_vertical"
android:orientation="horizontal">

<ImageView
android:layout_width="45dp"
android:layout_height="match_parent"
android:scaleType="center"
android:src="@mipmap/zhifubao"/>

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Alipay"
android:textColor="@color/black"
android:textSize="14sp"/>

<ImageView
android:id="@+id/Alipay_click"
android:layout_width="45dp"
android:layout_height="match_parent"
android:scaleType="center"
android:src="@drawable/circle_select_bg"/>
</LinearLayout>

<View style="@style/myline"/>

<TextView
android:id="@+id/popup_cancel"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:text="@string/finish"
android:textColor="@color/black"
android:textSize="14sp"/>

<View style="@style/myline"/>
</LinearLayout>
</RelativeLayout>


第二步:创建类继承PopupWindow:

package com.example.stamp.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.stamp.R;

/**
* Created by Administrator on 2016/8/18.
*/
public class SelectPayPopupWindow extends PopupWindow {

private  View mPayView;
private ImageView mWechat;
private ImageView mAlipay;
private TextView mCancel;

/**
* 这里的思想注意一下:这里直接把监听传入进来 这样便于外部实现
*
* @param context         上下文
* @param onClickListener 监听
*/
public SelectPayPopupWindow(Context context, View.OnClickListener onClickListener) {
super(context);

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mPayView = inflater.inflate(R.layout.firmorder_pay_popup, null);

setView();
initView();
initListener(onClickListener);
}

/**
* 设置显示参数
*/
private void setView() {
//设置PopupWindow要显示的View
this.setContentView(mPayView);
//设置PopupWindow弹出窗体的宽
this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//设置PopupWindow弹出窗体的高
this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//设置PopupWindow弹出窗体可点击
this.setFocusable(true);
//设置SelectPicPopupWindow弹出窗体动画效果
this.setAnimationStyle(R.style.AnimBottom);
//实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//设置PopupWindow弹出窗体的背景
this.setBackgroundDrawable(dw);
}

private void initView() {
mWechat = (ImageView) mPayView.findViewById(R.id.Wechat_click);
mAlipay = (ImageView) mPayView.findViewById(R.id.Alipay_click);
mCancel = (TextView) mPayView.findViewById(R.id.popup_cancel);
}

private void initListener(View.OnClickListener onClickListener) {
mWechat.setOnClickListener(onClickListener);
mAlipay.setOnClickListener(onClickListener);

mCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});

//mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
mPayView.setOnTouchListener(new View.OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {

int height = mPayView.findViewById(R.id.firmOrder).getTop();
int y=(int) event.getY();
if(event.getAction()==MotionEvent.ACTION_UP){
if(y<height){
dismiss();
}
}
return true;
}
});
}
}

第三步:编写类实现:这里贴的只是一个点击事件

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.FirmOrder_pay://支付方式
SelectPayPopupWindow payPopupWindow =  new SelectPayPopupWindow(this,mMenuOnclick );
payPopupWindow.showAtLocation(this.findViewById(R.id.FirmOrder), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
break;
}
}

private View.OnClickListener mMenuOnclick = new View.OnClickListener() {
@Override
public void onClick(View view) {
case R.id.Wechat_click://点击了微信按钮
MyToast.showShort(FirmOrderActivity.this,"点击了微信按钮");
break;
case R.id.Alipay_click://点击了支付宝按钮
MyToast.showShort(FirmOrderActivity.this,"点击了支付宝按钮");
break;
}};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: