1. 自定义BottomPayDialog
package com.example.xingchouwang.view.customView;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.xingchouwang.R;
import com.example.xingchouwang.controller.dao.PayClickListener;
/**
* [底部弹出dialog]
*
**/
public class BottomPayDialog extends Dialog implements View.OnClickListener {
private PayClickListener listener;
/**
* @param context
*/
public BottomPayDialog(Context context) {
super(context, R.style.MyDialog);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_dialog_pay);
Window window = getWindow();
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
layoutParams.dimAmount = 0.5f;
window.setGravity(Gravity.BOTTOM);
window.setAttributes(layoutParams);
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// 初始化控件
TextView close = (TextView) findViewById(R.id.tv_supportpay_close);
LinearLayout weixin = (LinearLayout) findViewById(R.id.ll_supportpay_weixin);
LinearLayout zhifubao = (LinearLayout) findViewById(R.id.ll_supportpay_zhifubao);
close.setOnClickListener(this);
weixin.setOnClickListener(this);
zhifubao.setOnClickListener(this);
//最重要的一句话,一定要加上!要不然怎么设置都不行
// dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
dismiss();
return true;
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.ll_supportpay_weixin) {
if (listener != null) {
dismiss();
listener.weixinPayListener(v);
}
return;
}
if (id == R.id.ll_supportpay_zhifubao) {
if (listener != null) {
dismiss();
listener.aliPayListener(v);
}
return;
}
if (id == R.id.tv_supportpay_close) {
// if (listener != null) {
// listener.onClick(v);
// }
dismiss();
return;
}
}
public void setPayClickListener(PayClickListener listener) {
this.listener = listener;
}
}
2. 在代码中使用
/**
* 显示支付窗口
*/
private void showDialog() {
BottomPayDialog bottomPayDialog = new BottomPayDialog(this);
bottomPayDialog.setPayClickListener(new PayClickListener() {
@Override
public void weixinPayListener(View v) {
KKKKK.showToast(SupportPayaddressActivity.this, "微信支付暂未开通,请使用其它支付方式");
}
@Override
public void aliPayListener(View v) {
aliPay();
}
});
bottomPayDialog.show();
}
3. 弹出框布局文件
<?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"
android:background="@color/bg_White"
android:orientation="vertical">
<View style="@style/style_greeenline" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/y80"
android:layout_alignTop="@+id/v1"
android:background="@color/bg_White"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/ll_supportpay_weixin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/custom_item_phototext_iv"
android:layout_width="@dimen/x40"
android:layout_height="@dimen/x40"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/y1"
android:src="@mipmap/weixinzhifu" />
<TextView
android:id="@+id/custom_item_phototext_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/y2"
android:gravity="center"
android:text="微信"
android:textColor="@color/green1"
android:textSize="@dimen/x11" />
</LinearLayout>
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="@color/green2" />
<LinearLayout
android:id="@+id/ll_supportpay_zhifubao"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="@dimen/x40"
android:layout_height="@dimen/x40"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/y1"
android:src="@mipmap/zhifubaozhifu" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/y2"
android:gravity="center"
android:text="支付宝"
android:textColor="@color/green1"
android:textSize="@dimen/x11" />
</LinearLayout>
</LinearLayout>
<View style="@style/style_greeenline" />
<TextView
android:id="@+id/tv_supportpay_close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:padding="@dimen/y6"
android:text="取消"
android:textColor="@color/green1"
android:textSize="@dimen/x16" />
<View
style="@style/style_greeenline"
android:layout_marginBottom="@dimen/y10" />
</LinearLayout>
4. 自定义style
<style name="MyDialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>