搜索相关关键字网上一大堆实现,但是看完总觉得缺胳膊少腿,绕了不少弯路,终于弄好了自定义dialog。把自己整合的完整代码发上来。
要点:
1、设置自定义dialog的布局文件my_dialog.xml
2、设置一份自定义的样式文件styles_wx.xml,该文件用于覆盖android的默认主题样式,如黑色边框等。
3、java代码继承Dialog实现自定义类MyDialog,实现自定义布局,还有设置窗口的大小、位置等。
(网上文章要么少介绍第2点,要么是使用AlterDialog直接实现效果)
先看下效果,模仿微信风格的dialog:
允许添加子view到弹出dialog,如:
代码。
Part1.styles_wx.xml
- @drawable/transparent
这是设置对话框弹出背景,尝试设置@null,仍然是黑色背景,在使用半透明图片时会受其影响。
所以,可以在这里指定你想要的背景图片或者颜色。
我为了灵活性,我设置该属性为名为"transparent"的图片,这是一张1*1的透明图片。这样背景完全透明,真正使用的背景在my_dialog.xml里定义
Part2.my_dialog.xml
package com.kwws;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.navigator.R;
/**
* 自定义对话框
*
* @author Kangwei
*
*/
public class MyDialog extends Dialog {
/* 属性 */
// 数据
String title = "title";
String text = "text";
String cancelButtonText = "cancel";
String okButtonText = "ok";
int okColor = -1;
int cancelColor = -1;
// UI
Context mContent;
TextView tvTitle;
TextView tvSeparator;
TextView tvText;
TextView tvBtn1;
TextView tvBtn2;
RelativeLayout childViewWrapper;// 子组件容器
/**
* 设置对话框样式,设置null则不显示
*
* @param context
* 上下文
* @param title
* 标题
* @param text
* 文本
* @param cancelButtonText
* 取消按钮文本
* @param okButtonText
* 确认按钮文本
*/
public MyDialog(Context context, String title, String text,
String cancelButtonText, String okButtonText) {
super(context, R.style.wx_dialog);// 样式定义,该样式去除android默认的黑色背景边框等。
this.mContent = context;
setDialogStyle(title, text, cancelButtonText, okButtonText);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LayoutInflater layout = LayoutInflater.from(mContent);
View view = layout.inflate(R.layout.my_dialog, null);
setContentView(view);
findView();
initView();
initViewEvent();
}
private void setDialogStyle(String title, String text,
String cancelButtonText, String okButtonText) {
this.title = title;
this.text = text;
this.cancelButtonText = cancelButtonText;
this.okButtonText = okButtonText;
}
private void findView() {
tvTitle = (TextView) findViewById(R.id.tvTitle);
tvSeparator = (TextView) findViewById(R.id.tvSeparator);
tvText = (TextView) findViewById(R.id.tvText);
tvBtn1 = (TextView) findViewById(R.id.tvButton1);
tvBtn2 = (TextView) findViewById(R.id.tvButton2);
childViewWrapper = (RelativeLayout) findViewById(R.id.rlContent);
}
private void initView() {
if (title == null) {
tvTitle.setVisibility(View.GONE);
tvSeparator.setVisibility(View.GONE);
} else {
tvTitle.setVisibility(View.VISIBLE);
tvSeparator.setVisibility(View.VISIBLE);
tvTitle.setText(title);
}
if (text == null) {
tvText.setVisibility(View.GONE);
} else {
tvText.setVisibility(View.VISIBLE);
tvText.setText(text);
}
if (cancelButtonText == null) {
tvBtn1.setVisibility(View.GONE);
} else {
tvBtn1.setVisibility(View.VISIBLE);
tvBtn1.setText(cancelButtonText);
if (cancelColor != -1) {
tvBtn1.setTextColor(cancelColor);
}
}
if (okButtonText == null) {
tvBtn2.setVisibility(View.GONE);
} else {
tvBtn2.setVisibility(View.VISIBLE);
tvBtn2.setText(okButtonText);
if (okColor != -1) {
tvBtn2.setTextColor(okColor);
}
}
if (childViewWrapper != null && childView != null) {
childViewWrapper.addView(childView);
}
// 设置对话框大小
Window dialogWindow = getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
DisplayMetrics d = mContent.getResources().getDisplayMetrics();
// 获取屏幕宽、高用
lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.8
dialogWindow.setAttributes(lp);
}
private void initViewEvent() {
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.tvButton1:
if (mListener != null) {
mListener.onCancelButtonClick(MyDialog.this, v);
}
break;
case R.id.tvButton2:
if (mListener != null) {
mListener.onOKButtonClick(MyDialog.this, v);
}
break;
default:
break;
}
}
};
tvBtn1.setOnClickListener(listener);
tvBtn2.setOnClickListener(listener);
}
public interface OnDialogButtonClickListener {
void onCancelButtonClick(MyDialog dialog, View view);
void onOKButtonClick(MyDialog dialog, View view);
}
private OnDialogButtonClickListener mListener;
/**
* 对话框按钮监听
*
* @param listener
*/
public void setOnDialogButtonClickListener(
OnDialogButtonClickListener listener) {
this.mListener = listener;
}
/**
* 确定按钮文本颜色
*
* @param color
*/
public void setOKButtonTextColor(int color) {
this.okColor = color;
}
/**
* 取消按钮文本颜色
*
* @param color
*/
public void setCancelButtonTextColor(int color) {
this.cancelColor = color;
}
View childView;
/**
* 在对话框显示自定义视图
*/
public void setChildView(View view) {
childView = view;
}
}
// 退出提示框
public void exitDialog() {
MyDialog dialog = new MyDialog(this, "提示", "确认退出?", "取消", "退出");
dialog.setOKButtonTextColor(getResources().getColor(R.color.red));
dialog.setOnDialogButtonClickListener(new OnDialogButtonClickListener() {
@Override
public void onOKButtonClick(MyDialog dialog, View view) {
finish();
}
@Override
public void onCancelButtonClick(MyDialog dialog, View view) {
dialog.dismiss();
}
});
dialog.show();
}
/*
* 显示配置对话框
*/
private void showConfigDialog() {
// 读取参数
final SharedPreferencesHelper helper = new SharedPreferencesHelper(
this, "config");
final String ip = helper.getValue("serverip");
final String port = helper.getValue("serverport");
// 配置界面 输入IP和端口的简单界面,这里就不附xml了
View view = getLayoutInflater().inflate(R.layout.dialog_config, null);
final EditText etIP = (EditText) view.findViewById(R.id.etIP);
final EditText etPort = (EditText) view.findViewById(R.id.etPort);
etIP.setText(ip != null ? ip : Ksoap2Helper.getServerIP());
etPort.setText(port != null ? port : String.valueOf(Ksoap2Helper
.getServerPort()));
// 配置对话框
MyDialog dialog = new MyDialog(this, "参数配置", null, "取消", "确定");
// 添加配置界面到对话框
dialog.setChildView(view);
// 按钮监听
dialog.setOnDialogButtonClickListener(new OnDialogButtonClickListener() {
@Override
public void onOKButtonClick(MyDialog dialog, View view) {
// 保存配置
String newIP = etIP.getText().toString();
String newPort = etPort.getText().toString();
if (true) {
helper.putValue("serverip", newIP);
Ksoap2Helper.setServerIP(newIP);
}
if (true) {
helper.putValue("serverport", newPort);
Ksoap2Helper.setServerPort(Integer.valueOf(newPort));
}
dialog.dismiss();
}
@Override
public void onCancelButtonClick(MyDialog dialog, View view) {
dialog.dismiss();
}
});
dialog.show();
}