Android 自定义AlertDialog对话框

系统默认的AlertDialog,与项目的UI不统一,所以,改了一下,定义了一样式,最终效果如下图:

另外,为了尽量少改原来的代码,该类类名及相关方法名都与android.app.AlertDialog相同,使用时,原代码只需要修改导入的包名,在按钮的Listener上加上一句关闭对话框的方法即可.
先是对话框的XML布局文件:
test.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- 顶部椭园边缘 -->
    <ImageView
        android:layout_width="400dp"
        android:layout_height="22dp"
        android:src="@drawable/dialog_top" >
    </ImageView>
	<!-- 中间白色背景,两个TextView,标题和内容,留一个LinearLayout,在代码中根据调用动态加上按钮 -->
    <LinearLayout
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:background="@drawable/dialog_center"
        android:orientation="vertical" >

        <TextView android:textColor="#000000"
            android:textSize="35dp"
            android:gravity="center"
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <TextView android:layout_marginLeft="20dp"
            android:layout_marginRight="10dp"
            android:id="@+id/message"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="20dp"
            android:textColor="#000000"
            android:textSize="25dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
		<!-- 在LinearLayout中加按钮 -->
        <LinearLayout
            android:id="@+id/buttonLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="horizontal" >
            
        </LinearLayout>
    </LinearLayout>
    <!-- 底部椭园边缘 -->
    <ImageView
        android:layout_marginTop="-2dp"
        android:layout_width="400dp"
        android:layout_height="22dp"
        android:src="@drawable/dialog_bottom" >
    </ImageView>

</LinearLayout>

按钮背景,不同状态不同,可参考本博相关文章:
alertdialog_button.xml


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/dialog_button_pressed" />
	<item android:state_focused="true" android:drawable="@drawable/dialog_button_pressed" />
	<item android:drawable="@drawable/dialog_button" />
</selector>

下面就是自定义的AlertDialog类的代码:


public class AlertDialog {
	Context context;
	android.app.AlertDialog ad;
	TextView titleView;
	TextView messageView;
	LinearLayout buttonLayout;
	public AlertDialog(Context context) {
		// TODO Auto-generated constructor stub
		this.context=context;
		ad=new android.app.AlertDialog.Builder(context).create();
		ad.show();
		//关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
		Window window = ad.getWindow();
		window.setContentView(R.layout.alertdialog);
		titleView=(TextView)window.findViewById(R.id.title);
		messageView=(TextView)window.findViewById(R.id.message);
		buttonLayout=(LinearLayout)window.findViewById(R.id.buttonLayout);
	}
	public void setTitle(int resId)
	{
		titleView.setText(resId);
	}
	public void setTitle(String title) {
		titleView.setText(title);
	}
	public void setMessage(int resId) {
		messageView.setText(resId);
	}

	public void setMessage(String message)
	{
		messageView.setText(message);
	}
	/**
	 * 设置按钮
	 * @param text
	 * @param listener
	 */
	public void setPositiveButton(String text,final View.OnClickListener listener)
	{
		Button button=new Button(context);
		LinearLayout.LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		button.setLayoutParams(params);
		button.setBackgroundResource(R.drawable.alertdialog_button);
		button.setText(text);
		button.setTextColor(Color.WHITE);
		button.setTextSize(20);
		button.setOnClickListener(listener);
		buttonLayout.addView(button);
	}
	
	/**
	 * 设置按钮
	 * @param text
	 * @param listener
	 */
	public void setNegativeButton(String text,final View.OnClickListener listener)
	{
		Button button=new Button(context);
		LinearLayout.LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		button.setLayoutParams(params);
		button.setBackgroundResource(R.drawable.alertdialog_button);
		button.setText(text);
		button.setTextColor(Color.WHITE);
		button.setTextSize(20);
		button.setOnClickListener(listener);
		if(buttonLayout.getChildCount()>0)
		{
			params.setMargins(20, 0, 0, 0);
			button.setLayoutParams(params);
			buttonLayout.addView(button, 1);
		}else{
			button.setLayoutParams(params);
			buttonLayout.addView(button);
		}
		
	}
	/**
	 * 关闭对话框
	 */
	public void dismiss() {
		ad.dismiss();
	}
	
}

使用方法:


				final AlertDialog ad=new AlertDialog(Test.this);
				ad.setTitle("标题");
				ad.setMessage("内容sdfsafdasf内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容");
				ad.setPositiveButton("确定", new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						ad.dismiss();
						Toast.makeText(Test.this, "被点到确定", Toast.LENGTH_LONG).show();
						
					}
				});
				
				ad.setNegativeButton("取消", new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						ad.dismiss();
						Toast.makeText(Test.this, "被点到取消", Toast.LENGTH_LONG).show();
					}
				});

© 2012, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记