自定义Dialog

来源:互联网 发布:白夜追凶国外知乎 编辑:程序博客网 时间:2024/05/17 07:37

1、layout - dialog_layout.xml

<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/dialoglayout"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:background="@color/dialog_bg"    android:fadingEdge="horizontal"    android:orientation="vertical" >    <TextView        android:id="@+id/title"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center_vertical"        android:paddingBottom="12.0dip"        android:paddingLeft="16.0dip"        android:paddingRight="16.0dip"        android:paddingTop="12.0dip"        android:text="阿萨德"        android:textColor="@color/dialog_title"        android:textSize="18.0sp" />    <ImageView        android:id="@+id/dialog_title_border"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@drawable/line_separate_blue" />    <LinearLayout        android:id="@+id/content"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="1.0"        android:gravity="center" >        <TextView            android:id="@+id/message"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_marginBottom="10.0dip"            android:layout_marginTop="10.0dip"            android:paddingLeft="30.0dip"            android:paddingRight="10.0dip"            android:text="阿萨德"            android:textColor="@android:color/black"            android:textSize="20.0dip"            android:visibility="visible" />    </LinearLayout>    <LinearLayout        android:id="@+id/mydialogbuttonlayout"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@color/dialog_btn_group_bg"        android:paddingRight="4.0dip" >        <Button            android:id="@+id/positiveButton"            style="@style/DialogBottomBtn"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_marginBottom="4.0dip"            android:layout_marginLeft="4.0dip"            android:layout_marginTop="4.0dip"            android:layout_weight="1.0"            android:text="阿萨德" />        <Button            android:id="@+id/neutralButton"            style="@style/DialogBottomBtn"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_marginBottom="4.0dip"            android:layout_marginLeft="4.0dip"            android:layout_marginTop="4.0dip"            android:layout_weight="1.0"            android:text="阿萨德" />        <Button            android:id="@+id/negativeButton"            style="@style/DialogBottomBtn"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_marginBottom="4.0dip"            android:layout_marginLeft="4.0dip"            android:layout_marginTop="4.0dip"            android:layout_weight="1.0"            android:text="阿萨德" />    </LinearLayout></LinearLayout>

2、styles.xml

<?xml version="1.0" encoding="UTF-8"?><resources>    <style name="Dialog" parent="android:style/Theme.Dialog">        <item name="android:windowNoTitle">true</item>        <item name="android:windowFrame">@null</item>        <item name="android:windowIsFloating">true</item>        <item name="android:windowIsTranslucent">false</item>    </style>    <style name="DialogText">        <item name="android:textColor">#FF000000</item>        <item name="android:textSize">12sp</item>    </style>    <style name="DialogText.Title">        <item name="android:textSize">20sp</item>        <item name="android:textColor">#FFFFFF</item>        <item name="android:textStyle">bold</item>    </style>    <style name="DialogBottomBtn">        <item name="android:textSize">18.0sp</item>        <item name="android:textColor">@android:color/black</item>        <item name="android:background">@drawable/window_btn_bg_selector</item>        <item name="android:paddingLeft">16.0dip</item>        <item name="android:paddingTop">12.0dip</item>        <item name="android:paddingRight">16.0dip</item>        <item name="android:paddingBottom">12.0dip</item>        <item name="android:minWidth">80.0dip</item>    </style></resources>

3、代码

public class MyDialog extends Dialog{        public MyDialog(Context context, int theme)    {        super(context, theme);    }        public MyDialog(Context context)    {        super(context);    }        public static class Builder    {        private Context context;                private String title;                private String message;                private int left, right, top, bottom;                private int buttonId = -1;                String[] items = null;                private int titleTextColor = Integer.MAX_VALUE;                private int buttonTextColor = Integer.MAX_VALUE;                private int buttonbackgroundId = -1;                private String positiveButtonText;                private String negativeButtonText;                private String neutralButtonText;                private View contentView;                private Button postiveButton;                private DialogInterface.OnClickListener positiveButtonClickListener, negativeButtonClickListener,            neutralButtonClickListener, itemButtonListener;                public Builder(Context context)        {            this.context = context;        }                public Builder setTitleTextColor(int titleTextColor)        {            this.titleTextColor = titleTextColor;            return this;        }                public Builder setMessage(String message)        {            this.message = message;            return this;        }                public Builder setButtonTextColor(int buttonTextColor)        {            this.buttonTextColor = buttonTextColor;            return this;        }                public Builder setMessage(int message)        {            this.message = (String)context.getText(message);            return this;        }                public Builder setTitle(int title)        {            this.title = (String)context.getText(title);            return this;        }                public Builder setButtonBackground(int buttonbackgroundId)        {            this.buttonbackgroundId = buttonbackgroundId;            return this;        }                public Builder setButtonTheme(int buttonId)        {            this.buttonId = buttonId;            return this;        }                public Builder setTitle(String title)        {            this.title = title;            return this;        }                public Builder setView(View v)        {            this.contentView = v;            return this;        }                public Builder setView(View v, int left, int top, int right, int bottom)        {            this.contentView = v;            this.left = left;            this.top = top;            this.right = right;            this.bottom = bottom;            return this;        }                public Builder setItem(String items[], OnClickListener listener)        {            this.items = items;            itemButtonListener = listener;            return this;        }                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 Builder setNeutralButton(int neutralButtonText, DialogInterface.OnClickListener listener)        {            this.neutralButtonText = (String)context.getText(neutralButtonText);            this.neutralButtonClickListener = listener;            return this;        }                public Builder setNeutralButton(String neutralButtonText, DialogInterface.OnClickListener listener)        {            this.neutralButtonText = neutralButtonText;            this.neutralButtonClickListener = listener;            return this;        }                public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener)        {            this.negativeButtonText = (String)context.getText(negativeButtonText);            this.negativeButtonClickListener = listener;            return this;        }                public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener)        {            this.negativeButtonText = negativeButtonText;            this.negativeButtonClickListener = listener;            return this;        }                public MyDialog create()        {            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            final MyDialog Mydialog = new MyDialog(context, R.style.Dialog);            View layout = inflater.inflate(R.layout.dialog_layout, null);            postiveButton = ((Button)layout.findViewById(R.id.positiveButton));            if (titleTextColor != Integer.MAX_VALUE)            {                ((TextView)layout.findViewById(R.id.title)).setTextColor(titleTextColor);            }            if (buttonTextColor != Integer.MAX_VALUE)            {                postiveButton.setTextColor(buttonTextColor);                ((Button)layout.findViewById(R.id.negativeButton)).setTextColor(buttonTextColor);                ((Button)layout.findViewById(R.id.neutralButton)).setTextColor(buttonTextColor);            }            if (buttonId != -1)            {                postiveButton.setBackgroundResource(buttonId);                ((Button)layout.findViewById(R.id.negativeButton)).setBackgroundResource(buttonId);                ((Button)layout.findViewById(R.id.neutralButton)).setBackgroundResource(buttonId);            }            if (buttonbackgroundId != -1)            {                ((LinearLayout)layout.findViewById(R.id.mydialogbuttonlayout)).setBackgroundResource(buttonbackgroundId);            }            Mydialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));            if (title != null)            {                                ((TextView)layout.findViewById(R.id.title)).setText(title);            }            else            {                ((LinearLayout)layout.findViewById(R.id.mydiatitlelayout)).setVisibility(View.GONE);            }            if (positiveButtonText != null)            {                postiveButton.setText(positiveButtonText);                if (positiveButtonClickListener != null)                {                    postiveButton.setOnClickListener(new View.OnClickListener()                    {                        public void onClick(View v)                        {                            positiveButtonClickListener.onClick(Mydialog, DialogInterface.BUTTON_POSITIVE);                            postiveButton.setClickable(false);                            Mydialog.dismiss();                        }                    });                }            }            else            {                layout.findViewById(R.id.positiveButton).setVisibility(View.GONE);            }            if (neutralButtonText != null)            {                ((Button)layout.findViewById(R.id.neutralButton)).setText(neutralButtonText);                if (neutralButtonClickListener != null)                {                    ((Button)layout.findViewById(R.id.neutralButton)).setOnClickListener(new View.OnClickListener()                    {                        public void onClick(View v)                        {                            neutralButtonClickListener.onClick(Mydialog, DialogInterface.BUTTON_NEUTRAL);                            Mydialog.dismiss();                        }                    });                }            }            else            {                layout.findViewById(R.id.neutralButton).setVisibility(View.GONE);            }            if (negativeButtonText != null)            {                ((Button)layout.findViewById(R.id.negativeButton)).setText(negativeButtonText);                if (negativeButtonClickListener != null)                {                    ((Button)layout.findViewById(R.id.negativeButton)).setOnClickListener(new View.OnClickListener()                    {                        public void onClick(View v)                        {                            negativeButtonClickListener.onClick(Mydialog, DialogInterface.BUTTON_NEGATIVE);                            Mydialog.dismiss();                        }                    });                }            }            else            {                layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);            }            if (negativeButtonText == null && neutralButtonText == null && positiveButtonText == null)            {                ((LinearLayout)layout.findViewById(R.id.mydialogbuttonlayout)).setVisibility(View.GONE);            }            if (message != null)            {                ((TextView)layout.findViewById(R.id.message)).setText(message);            }            else            {                ((TextView)layout.findViewById(R.id.message)).setVisibility(View.GONE);            }            if (items != null && items.length > 0)            {                LinearLayout selflayout = ((LinearLayout)layout.findViewById(R.id.content));                selflayout.removeAllViews();                for (int i = 0; i < items.length; i++)                {                    final int pos = i;                    TextView text = new TextView(context);                    text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 80));                    text.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);                    text.setTextSize(16);                    text.setTextColor(Color.BLACK);                    text.setBackgroundColor(Color.WHITE);                    text.setPadding(30, 0, 0, 0);                    text.setText(items[i]);                    selflayout.addView(text);                    if (i != items.length - 1)                    {                        ImageView textline = new ImageView(context);                        textline.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));                        textline.setImageResource(R.drawable.dialog_diverse);                        selflayout.addView(textline);                    }                    if (itemButtonListener != null)                    {                        text.setOnClickListener(new View.OnClickListener()                        {                            @Override                            public void onClick(View v)                            {                                itemButtonListener.onClick(Mydialog, pos);                                Mydialog.dismiss();                            }                        });                    }                }            }            else if (contentView != null)            {                ((LinearLayout)layout.findViewById(R.id.content)).removeAllViews();                ((LinearLayout)layout.findViewById(R.id.content)).setPadding(left, top, right, bottom);                ((LinearLayout)layout.findViewById(R.id.content)).addView(contentView, new LayoutParams(                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));            }            Mydialog.setContentView(layout);            return Mydialog;        }    }}

4、应用

clickme.setOnClickListener(new OnClickListener(){     @Override     public void onClick(View v)     {MyDialog.Builder myDialog = new MyDialog.Builder(MainA.this);myDialog.setTitle("Farmer");myDialog.setMessage("小强好帅!");// 可以复用中间的content页面//LayoutInflater inflater = (LayoutInflater)getSystemService(Service.LAYOUT_INFLATER_SERVICE);//View view = inflater.inflate(R.layout.third, null);//myDialog.setView(view);myDialog.setPositiveButton("OK", new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which){}});myDialog.setNeutralButton("hha", new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which){}});myDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which){}});myDialog.create().show();     }});

5、截图显示

        

6、有些机型,把Dialog改掉了,使用自定义的会有很难看的边框,可以改一下Dialog的样式,如下:

<style name="Dialog" parent="android:style/Theme.Dialog"><item name="android:windowNoTitle">true</item><item name="android:windowFrame">@null</item>  <item name="android:windowIsFloating">true</item>  <item name="android:windowIsTranslucent">false</item>  <item name="android:background">@android:color/black</item>  <item name="android:windowBackground">@null</item></style>