自定义dialog布局

效果预览:

Customdialog.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:background="@drawable/dialogbg"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/dialogradius"
        android:gravity="center"
        android:text=""
        android:textColor="#FFF"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/error_hint"
        android:layout_width="match_parent"
        android:layout_height="68dp"
        android:gravity="center"
        android:text=""
        android:textSize="18dp" />

    <TextView
        android:id="@+id/error_confirm"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/shape_yuanjaio_green"
        android:gravity="center"
        android:text="确定"
        android:textColor="#FFF"
        android:textSize="20dp" />

</LinearLayout>  

Dialogradius.xml(shape使用):

<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android" >  
    <solid android:color="#85C226" />
    <corners
        android:bottomLeftRadius="1dp"
        android:bottomRightRadius="1dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />
</shape>  

Dialog.xml(shape使用):

<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android" >  
    <solid android:color="#fff" />
    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />

</shape>  

shapeyuanjaiogreen.xml(shape使用)

<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android" >  
    <corners android:radius="5dp"/>
    <solid android:color="#85C226"/>
</shape>  

代码:

                                        LayoutInflater inflaterDl = LayoutInflater.from(mcontext);
                                layout = (LinearLayout) inflaterDl.inflate( R.layout.customdialog, null);
                            final Dialog dialog = new AlertDialog.Builder(GoodDetailActivity.this).create();
                        TextView tv = (TextView)    layout.findViewById(R.id.error_hint);
                    tv.setText(“该商品已经缺货”);
                    dialog.show();
                    dialog.setContentView(layout);
                    dialog.setCancelable(false);
                    Window dialogWindow = dialog.getWindow();
                    dialogWindow.setGravity(Gravity.CENTER);
                    dialogWindow.setLayout(1000, 550);

                    error_confirm = (TextView) layout
                            .findViewById(R.id.error_confirm);

                    error_confirm.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            dialog.dismiss();

                        }

                    });