直接上代碼
public class CustomDialog {
private Context mContext;
protected Dialog dialog;
private LinearLayout mBackgroundLayout;
private LinearLayout mMainLayout;
private TextView mTitleTv;
private Button mCancelButton;
private Button mSureButton;
private ImageView mLineImg;
private Display mDisplay;
/**
* 是否顯示title
*/
private boolean showTitle = false;
/***
* 是否顯示確定按鈕
*/
private boolean showPosBtn = false;
/**
* 是否顯示取消按鈕
*/
private boolean showNegBtn = false;
/**
* dialog 寬度
*/
private float dialogWidth = 0.7f;
public MUIKitDialog(Context context) {
this.mContext = context;
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mDisplay = windowManager.getDefaultDisplay();
}
public MUIKitDialog builder() {
View view = LayoutInflater.from(mContext).inflate(R.layout.view_dialog, null);
mBackgroundLayout = (LinearLayout) view.findViewById(R.id.ll_background);
mMainLayout = (LinearLayout) view.findViewById(R.id.ll_alert);
mMainLayout.setVerticalGravity(View.GONE);
mTitleTv = (TextView) view.findViewById(R.id.tv_title);
mTitleTv.setVisibility(View.GONE);
mCancelButton = (Button) view.findViewById(R.id.btn_neg);
mCancelButton.setVisibility(View.GONE);
mSureButton = (Button) view.findViewById(R.id.btn_pos);
mSureButton.setVisibility(View.GONE);
mLineImg = (ImageView) view.findViewById(R.id.img_line);
mLineImg.setVisibility(View.GONE);
dialog = new Dialog(mContext, R.style.TUIKit_AlertDialogStyle);
dialog.setContentView(view);
mBackgroundLayout.setLayoutParams(new FrameLayout.LayoutParams((int) (mDisplay.getWidth() * dialogWidth), LayoutParams.WRAP_CONTENT));
return this;
}
public MUIKitDialog setTitle(@NonNull String title) {
showTitle = true;
mTitleTv.setText(title);
return this;
}
/***
* 是否點(diǎn)擊返回能夠取消
* @param cancel
* @return
*/
public MUIKitDialog setCancelable(boolean cancel) {
dialog.setCancelable(cancel);
return this;
}
/**
* 設(shè)置是否可以取消
*
* @param isCancelOutside
* @return
*/
public MUIKitDialog setCancelOutside(boolean isCancelOutside) {
dialog.setCanceledOnTouchOutside(isCancelOutside);
return this;
}
/**
* 設(shè)置確定
*
* @param text
* @param listener
* @return
*/
public MUIKitDialog setPositiveButton(String text,
final OnClickListener listener) {
showPosBtn = true;
mSureButton.setText(text);
mSureButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(v);
dialog.dismiss();
}
});
return this;
}
public MUIKitDialog setPositiveButton(final OnClickListener listener) {
setPositiveButton("確定", listener);
return this;
}
/***
* 設(shè)置取消
* @param text
* @param listener
* @return
*/
public MUIKitDialog setNegativeButton(String text,
final OnClickListener listener) {
showNegBtn = true;
mCancelButton.setText(text);
mCancelButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(v);
dialog.dismiss();
}
});
return this;
}
public MUIKitDialog setNegativeButton(
final OnClickListener listener) {
setNegativeButton("取消", listener);
return this;
}
private void setLayout() {
if (!showTitle) {
mTitleTv.setVisibility(View.GONE);
}
if (showTitle) {
mTitleTv.setVisibility(View.VISIBLE);
}
if (!showPosBtn && !showNegBtn) {
mSureButton.setVisibility(View.GONE);
mSureButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
if (showPosBtn && showNegBtn) {
mSureButton.setVisibility(View.VISIBLE);
mCancelButton.setVisibility(View.VISIBLE);
mLineImg.setVisibility(View.VISIBLE);
}
if (showPosBtn && !showNegBtn) {
mSureButton.setVisibility(View.VISIBLE);
}
if (!showPosBtn && showNegBtn) {
mCancelButton.setVisibility(View.VISIBLE);
}
}
public void show() {
setLayout();
dialog.show();
}
public void dismiss() {
dialog.dismiss();
}
/**
* 設(shè)置dialog 寬度
*
* @param dialogWidth
* @return
*/
public MUIKitDialog setDialogWidth(float dialogWidth) {
if (mBackgroundLayout != null) {
mBackgroundLayout.setLayoutParams(new FrameLayout.LayoutParams((int) (mDisplay.getWidth() * dialogWidth), LayoutParams.WRAP_CONTENT));
}
this.dialogWidth = dialogWidth;
return this;
}
/***
* 獲取title
* @return
*/
public TextView getTxt_title() {
return mTitleTv;
}
/**
* 獲取確定按鈕
*
* @return
*/
public Button getBtn_neg() {
return mCancelButton;
}
/***
* 獲取用于添加自定義控件的ll
* @return
*/
public LinearLayout getlLayout_alert_ll() {
return mMainLayout;
}
/***
* 根據(jù)手機(jī)的分辨率從 dip 的單位 轉(zhuǎn)成為 px(像素)
* @param dpValue
* @return
*/
public int dp2px(float dpValue) {
final float scale = mContext.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根據(jù)手機(jī)的分辨率從 px(像素) 的單位 轉(zhuǎn)成為 dp
*/
public int px2dip(float pxValue) {
final float scale = mContext.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 將px值轉(zhuǎn)換為sp值哲虾,保證文字大小不變
*/
public int px2sp(float pxValue) {
final float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
/**
* 將sp值轉(zhuǎn)換為px值祠够,保證文字大小不變
*/
public int sp2px(float spValue) {
final float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
/**
* 獲取取消按鈕
*
* @return
*/
public Button getBtn_pos() {
return mSureButton;
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/alert_bg"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:gravity="center"
android:textColor="@color/black"
android:textSize="@dimen/text_size_16sp"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/ll_alert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<ImageView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:background="@color/dialog_line_bg" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_neg"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@drawable/trans_bg"
android:gravity="center"
android:textColor="@color/font_blue"
android:textSize="@dimen/text_size_16sp" />
<ImageView
android:id="@+id/img_line"
android:layout_width="0.5dp"
android:layout_height="50dp"
android:background="@color/dialog_line_bg" />
<Button
android:id="@+id/btn_pos"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@drawable/trans_bg"
android:gravity="center"
android:textColor="@color/font_blue"
android:textSize="@dimen/text_size_16sp" />
</LinearLayout>
</LinearLayout>