自定义Dialog,两种方式

package com.example.alertdialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.app.AlertDialog;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
	    final AlertDialog dialog=new AlertDialog.Builder(this).create();
	    View dialogview=LayoutInflater.from(this).inflate(R.layout.newdialogxml, null);
		dialog.setView(dialogview);
		dialog.setTitle("自定义Dialog1");
		((Button)(findViewById(R.id.action_settings))).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.show();
			}
		});
		((Button)(dialogview.findViewById(R.id.button))).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
		
		((Button)(findViewById(R.id.action_settings2))).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ShowDialogtwo();
			}

			private void ShowDialogtwo() {
				// TODO Auto-generated method stub
				final AlertDialog dialog=new AlertDialog.Builder(MainActivity.this).create();
				dialog.show();
				Window window=dialog.getWindow();
				/*
				 * getwindow后背景会变成灰色,此3行代码给背景相当于给了给透明色
				 * */
	            WindowManager.LayoutParams params = window.getAttributes();  
	            params.dimAmount = 0f;  
	            window.setAttributes(params);  
	            
				window.setContentView(R.layout.newdialogxml);
				window.setTitle("自定义Dialog2");
				
				
				((Button)(window.findViewById(R.id.button))).setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						dialog.cancel();
					}
				});
				
			}
		});
			
	}

	

}

  

<?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="wrap_content"
    android:orientation="vertical" >
    
<TextView 
    android:gravity="center"
    android:layout_gravity="center_vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="这是一个自定义dialog"/>
<Button
    android:id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="取消"/>
</LinearLayout>

  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
	 android:orientation="vertical">

    <Button
        android:id="@+id/action_settings"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="查看自定义Dialog1" />
        <Button
        android:id="@+id/action_settings2"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="查看自定义Dialog2" />

</LinearLayout>

  

原文地址:https://www.cnblogs.com/androidxufeng/p/3615747.html