android 自定义对话框

想要自己设计对话框的话

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="android: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就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值