在android开发中我们经常会用到对话框,系统提供的弹出对话框有时并不能满足我们的开发需求,而且各个厂商定制原因,可能每种手机弹出的对话框都长的不一样,这时我们就需要自定义对话框了,今天要定义一种对话框保证其在各个版本手机上长得一样,各种圆角背景可以随意搭配,完全可以满足你日常的开发需求,以代码说话先看最终的效果图
至于你想要更好看的对话框只能自己去定义你的UI,这里我只是简单的做一个作为例子,一下为示例代码
1、在styles文件中定义对话框样式
<style name="MyDialog" parent="@android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/dialog_bg</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
2、对话框布局文件dialog.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="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:id="@+id/tv_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
android:gravity="center"
android:text="标题"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
<TextView
android:id="@+id/tv_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:layout_marginBottom="15dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="内容"
android:textColor="#000000"
android:textSize="15sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom|center_horizontal"
android:paddingBottom="10dip"
android:paddingTop="10dip" >
<Button
android:id="@+id/confirm"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1"
android:background="@drawable/btn_bg1"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:text="确定"
android:textColor="#ffffff"
android:textSize="18sp"/>
<Button
android:id="@+id/cancel"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1"
android:background="@drawable/btn_bg2"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:text="取消"
android:textColor="#ffffff"
android:textSize="18sp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
3、在drawable文件下下定义对话框背景dialog.xml,确定和取消按钮背景btn_bg1.xml和btn_bg2.xml
dialog.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 填充的颜色 -->
<solid android:color="#ffffff" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="8dip" />
</shape>
btn_bg1.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 填充的颜色 -->
<solid android:color="#d01716" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="8dip" />
</shape>
btn_bg2.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 填充的颜色 -->
<solid android:color="#aaaaaa" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="8dip" />
</shape>
4、自定义CustomDialog.java代码
package com.example.customdialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CustomDialog extends Dialog {
private Button confirm;
private Button cancel;
private TextView tv_title;
private TextView tv_message;
private String titleStr;
private String messageStr;
private String confirmStr;
private String cancelStr;
private OnConfirmClickListener onConfirmClickListener;
private OnCancelClickListener onCancelClickListener;
public CustomDialog(Context context) {
super(context, R.style.MyDialog);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.dialog);
setCanceledOnTouchOutside(true);
initView();
initData();
initEvent();
}
private void initView() {
confirm = (Button) findViewById(R.id.confirm);
cancel = (Button) findViewById(R.id.cancel);
tv_title = (TextView) findViewById(R.id.tv_title);
tv_message = (TextView) findViewById(R.id.tv_message);
}
private void initData() {
if (titleStr != null) {
tv_title.setText(titleStr);
}
if (messageStr != null) {
tv_message.setText(messageStr);
}
// 如果设置按钮的文字
if (confirmStr != null) {
confirm.setText(confirmStr);
}
if (cancelStr != null) {
confirm.setText(cancelStr);
}
}
private void initEvent() {
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogDismiss();
if (onConfirmClickListener != null) {
onConfirmClickListener.onConfirmClick();
}
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogDismiss();
if (onCancelClickListener != null) {
onCancelClickListener.onCancelClick();
}
}
});
}
private void dialogDismiss(){
this.dismiss();
}
public void setTitle(String title) {
titleStr = title;
}
public void setMessage(String message) {
messageStr = message;
}
public interface OnConfirmClickListener {
public void onConfirmClick();
}
public interface OnCancelClickListener {
public void onCancelClick();
}
public void setOnConfirmClickListener(String str,OnConfirmClickListener onConfirmClickListener) {
if (str != null) {
cancelStr = str;
}
this.onConfirmClickListener = onConfirmClickListener;
}
public void setOnCancelClickListener(String str,OnCancelClickListener onCancelClickListener) {
if (str != null) {
confirmStr = str;
}
this.onCancelClickListener = onCancelClickListener;
}
}
5、Activity中调用
CustomDialog dialog = new CustomDialog(MainActivity.this);
dialog.setTitle("温馨提示");
dialog.setMessage("您确定要删除此条记录吗?");
dialog.setOnConfirmClickListener("确定",new OnConfirmClickListener(){
@Override
public void onConfirmClick() {
}
});
dialog.setOnCancelClickListener("取消", new OnCancelClickListener(){
@Override
public void onCancelClick() {
}
});
dialog.show();
以上只是android中自定义Dialog的一种简单的实现代码,你可以按照自己的需求进行改造和封装.