一个简单的仿IOS 对话框, 感觉很好用 记录下来 往后可以直接转移到别的项目中使用。
先来看下效果图:
效果简单明了,感觉ios 的对话框就要比安卓原生的好看一些,下面附上代码:
public class AlertDialog extends Dialog
{
public AlertDialog(Context context)
{
super(context);
}
public AlertDialog(Context context, int theme)
{
super(context, theme);
}
/**
* 建造者类
*
* @author Administrator
*
*/
public static class Builder
{
private Context context;
private String title;
private String message;
private String positiveButtonText;
private String negativeButtonText;
private View contentView;
private OnClickListener positiveButtonClickListener;
private OnClickListener negativeButtonClickListener;
public Builder(Context context)
{
this.context = context;
}
/**
* 设置消息内容
*
* @param message
* @return
*/
public Builder setMessage(String message)
{
this.message = message;
return this;
}
/**
* 设置消息标题
*
* @param title
* @return
*/
public Builder setTitle(String title)
{
this.title = title;
return this;
}
public Builder setContentView(View v)
{
this.contentView = v;
return this;
}
/**
* 设置积极按钮
*
* @param positiveButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(String positiveButtonText,
OnClickListener listener)
{
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}
/**
* 设置消极按钮
*
* @param negativeButtonText
* @param listener
* @return
*/
public Builder setNegativeButton(String negativeButtonText,
OnClickListener listener)
{
this.negativeButtonText = negativeButtonText;
this.negativeButtonClickListener = listener;
return this;
}
/**
* 创建一个AlertDialog
*
* @return
*/
public AlertDialog create()
{
LayoutInflater inflater = LayoutInflater.from(context);
final AlertDialog dialog = new AlertDialog(context,
R.style.DialogStyle);
View layout = null;
if (null != contentView)
{
layout = contentView;
} else
{
layout = inflater.inflate(R.layout.layout_alert_dialog, null);
}
// 设置标题
TextView titleView = (TextView) layout.findViewById(R.id.title);
if (null == title)
{
titleView.setVisibility(View.GONE);
} else
{
titleView.setText(title);
}
// 设置内容
TextView messageView = (TextView) layout.findViewById(R.id.message);
if (null == message)
{
messageView.setVisibility(View.GONE);
} else
{
messageView.setText(message);
}
// 设置积极按钮
Button positiveButton = (Button) layout
.findViewById(R.id.positiveButton);
if (null == positiveButtonText
|| null == positiveButtonClickListener)
{
positiveButton.setVisibility(View.GONE);
} else
{
positiveButton.setText(positiveButtonText);
positiveButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
dialog.dismiss();
positiveButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
// 设置消极按钮
Button negativeButton = (Button) layout
.findViewById(R.id.negativeButton);
if (null == negativeButtonText
|| null == negativeButtonClickListener)
{
negativeButton.setVisibility(View.GONE);
} else
{
negativeButton.setText(negativeButtonText);
negativeButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
dialog.dismiss();
negativeButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_NEGATIVE);
}
});
}
if (null != positiveButtonText && null == negativeButtonText)
{
positiveButton
.setBackgroundResource(R.drawable.selector_single_btn);
if (null == positiveButtonClickListener)
{
positiveButton.setVisibility(View.VISIBLE);
positiveButton.setText(positiveButtonText);
positiveButton
.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
dialog.dismiss();
}
});
}
}
// 设置对话框的视图
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
dialog.setContentView(layout, params);
return dialog;
}
}
}
重点是这里,我比较了透明的效果和全灰色的效果,感觉透明的效果更好看一些,也可以根据自己的需要更改颜色 。
下面附上Dialog 的layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="20dp"
>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="标题"
android:textColor="#000"
android:textSize="@dimen/textNormalSize"
android:textStyle="bold"
android:layout_marginTop="20dp"
/>
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="消息内容"
android:textColor="@color/text_color_message"
android:textSize="@dimen/textLittleSize"
android:layout_marginTop="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:orientation="horizontal">
<Button
android:id="@+id/positiveButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_left_btn"
android:gravity="center"
android:padding="6dp"
android:text="确认"
android:textColor="@color/text_color_btn"
android:textSize="17dp"/>
<Button
android:id="@+id/negativeButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="@drawable/selector_right_btn"
android:gravity="center"
android:padding="6dp"
android:text="取消"
android:textColor="@color/text_color_btn"
android:textSize="17dp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>