您的位置:首页 > 移动开发 > Android开发

android 自定义对话框

2015-12-16 11:07 459 查看
想要自己设计对话框的话

1、在xml中设计自己想要的样式 在style中重写主题

2、新建一个java文件继承dialog 重写相应的方法

3、在现实的activity中调用自定义的对话框

演示效果:

下面是演示代码:

对话框的布局文件 xml:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/duty_dialog_style"
android:orientation="vertical" >

<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="3dp"
android:gravity="center"
android:text="值日提醒"
android:textColor="#FFFFFF"
android:textSize="18sp" />

<TextView
android:id="@+id/tv_CleanInformation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="3dp"
android:text="值日人员是:李林凯 庞田旺"
android:textColor="#FFFFFF"
android:textSize="16sp" />

<Button
android:id="@+id/bt_sure"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:background="@drawable/duty_dialog_style"
android:gravity="center"
android:text="好的老大"
android:textColor="#FFFFFF"
android:textSize="13sp" />

</LinearLayout>
效果:

drawable中xml文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 颜色 -->
<solid android:color="#60c325" />
<!-- 弯角 -->
<corners android:radius="20dp" />

<padding
android:bottom="10dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<!-- 边框 -->
<stroke
android:width="0.5dp"
android:color="#FFFFFF" >
</stroke>

</shape>style文件:
<resources>

<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.

-->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

<!-- 重写系统弹出Dialog -->
<style name="myDialogTheme" parent="android:Theme.Dialog">
<item name="android:background">#ffffff</item>

<item name="a
4000
ndroid:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<!-- 除去title -->
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowBackground">@null</item>
<!-- 除去背景色 -->

</style>

</resources>

对话框的java文件
*
*
*@auther Jianjun Huang
*
*@date 2015年12月11日
*/
public class Duty_Dialog extends Dialog {

public Duty_Dialog(Context context) {
super(context);

}

public Duty_Dialog(Context context, int theme) {
super(context, theme);
}

public static class Builder {
private String title;
private Context context;
private String message;
private String positiveButtonText;
private View contentView;
private DialogInterface.OnClickListener  positiveButtonClickListener;

public Builder(Context context) {
this.context = context;
}
/**
* 设置信息(string)
*
* @param message
* @return
*/
public Builder setMessage(String message) {
this.message = message;
return this;
}

/**
* 设置信息(resource)
*
* @param title
* @return
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}

public Builder setTitle(String title) {
this.title = title;
return this;
}

public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}

/**
*
* @param v
* @return
*/
public Builder setContentView(View v) {
this.contentView = v;
return this;
}
/**
*
* @param positiveButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {
this.positiveButtonText = (String) context.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}

public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}

public Duty_Dialog create(){
//LayoutInflater作用是将layout的xml布局文件实例化为View类对象  通过SystemService获得
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

final Duty_Dialog dialog = new Duty_Dialog(context, R.style.myDialogTheme);

View layout = inflater.inflate(R.layout.duty_dialog_style, null);
//添加布局
dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

((TextView) layout.findViewById(R.id.tv_title)).setText(title);
//设置按钮
if (positiveButtonText != null) {
((Button) layout.findViewById(R.id.bt_sure)).setText(positiveButtonText);

if (positiveButtonClickListener != null) {

((Button) layout.findViewById(R.id.bt_sure)).setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {

layout.findViewById(R.id.bt_sure).setVisibility(View.GONE);

}

if (message != null) {
((TextView) layout.findViewById(R.id.tv_CleanInformation)).setText(message);
}
else if (contentView != null) {

((LinearLayout) layout.findViewById(R.id.linearLayout_dialog)).removeAllViews();
((LinearLayout) layout.findViewById(R.id.linearLayout_dialog)).addView(contentView,
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
dialog.setContentView(layout);
return dialog;

}

}
}
activity的java文件
// 对话提醒
private void alterDiog(String cleanMember2, String weeks, String mWay) {

Duty_Dialog.Builder builder = new Duty_Dialog.Builder(MyApplication.getContext());

builder.setTitle("值日提醒");
// 设置信息
builder.setMessage("今天是" + "\t" + weeks + "\t" + mWay + "\n" + "值日人员:" + cleanMember2 + "\n大家互相提醒一下!");

builder.setPositiveButton("知道了", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

}
});

Window window = MyApplication.getWindow();
// 获取对话框当前的参数值
// WindowManager.LayoutParams p = window.getAttributes();
//
// WindowManager m = MyApplication.getWindow().getWindowManager();
// // 获取屏幕宽、高用
// Display d = m.getDefaultDisplay();
//
// // 宽度设置为屏幕的0.65
// p.width = (int) (d.getWidth() * 0.6);
//
// // 设置位置
// window.setGravity(Gravity.CENTER);
// // 透明度
// p.alpha = 0.7f;
// window.setAttributes(p);

Duty_Dialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);//
// 将弹出框设置为全局
dialog.setCanceledOnTouchOutside(false);// 失去焦点不会消失
dialog.show();
}

因为我的这个对话框要在全局显示所以才在最后加上了全局显示的部分
若不需要全局显示可以不用这样写 直接create后show就可以了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息