android自定义对话框

做个自定义的对话框,对话框类里写各种各样的对话框。以后不要写xml配置文件了:

package df.util.test;

import android.R;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import df.util.android.ResourceUtil;
import df.util.type.StringUtil;

/**
 * Created with IntelliJ IDEA.
 * User: wulong
 * Date: 13-8-15
 * Time: 下午5:21
 * To change this template use File | Settings | File Templates.
 */
public class DialogUtil {
    public enum Screen {wrap_screen, full_screen}

    public enum EGravityType {
        CENTER,
        TOP,
        BOTTOM,
        LEFT,
        RIGHT,
        CENTER_VERTICAL,
        CENTER_HORIZONTAL;

        public static int toRelativeGravity(EGravityType type) {
            switch (type) {
                case CENTER: {
                    return RelativeLayout.CENTER_IN_PARENT;
                }
                case TOP: {
                    return RelativeLayout.ALIGN_PARENT_TOP;
                }
                case BOTTOM: {
                    return RelativeLayout.ALIGN_PARENT_BOTTOM;
                }
                case LEFT: {
                    return RelativeLayout.ALIGN_PARENT_LEFT;
                }
                case RIGHT: {
                    return RelativeLayout.ALIGN_PARENT_RIGHT;
                }
                case CENTER_VERTICAL: {
                    return RelativeLayout.CENTER_VERTICAL;
                }
                case CENTER_HORIZONTAL: {
                    return RelativeLayout.CENTER_HORIZONTAL;
                }
                default: {
                    return RelativeLayout.CENTER_IN_PARENT;
                }
            }
        }
    }

    /**
     * 显示带背景图片的对话框,只有一个确认按钮
     *
     * @param act
     * @param screenType
     * @param bgDrawableName
     * @param okButtonDrawableName
     * @param buttonLayoutGravity
     */
    public static void showDrawableDialog(final Activity act,
                                          Screen screenType,
                                          String bgDrawableName,
                                          String okButtonDrawableName,
                                          final EGravityType buttonLayoutGravity[]
    ) {
        try {
            RelativeLayout relativeLayout = new RelativeLayout(act);
            relativeLayout.setPadding(0, 0, 0, 0);

            final ImageButton okBtn = new ImageButton(act);

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            for (EGravityType gravity : buttonLayoutGravity) {
                if (gravity == EGravityType.CENTER) {
                    params.addRule(RelativeLayout.CENTER_IN_PARENT);
                } else if (gravity == EGravityType.TOP) {
                    params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                } else if (gravity == EGravityType.BOTTOM) {
                    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                } else if (gravity == EGravityType.LEFT) {
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                } else if (gravity == EGravityType.RIGHT) {
                    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                } else if (gravity == EGravityType.CENTER_VERTICAL) {
                    params.addRule(RelativeLayout.CENTER_VERTICAL);
                } else if (gravity == EGravityType.CENTER_HORIZONTAL) {
                    params.addRule(RelativeLayout.CENTER_HORIZONTAL);
                }
            }

            relativeLayout.addView(okBtn, params);

            if (!StringUtil.empty(bgDrawableName)) {
                int bgDrawableId = ResourceUtil.getDrawableResourceIdFromName(act, bgDrawableName);
                relativeLayout.setBackgroundResource(bgDrawableId);
            }

            if (!StringUtil.empty(okButtonDrawableName)) {
                int okDrawableId = ResourceUtil.getDrawableResourceIdFromName(act, okButtonDrawableName);
                okBtn.setBackgroundResource(okDrawableId);
            }

            int full_wrap_screen_type = R.style.Theme_Light_NoTitleBar;
            if (Screen.full_screen == screenType) {
                full_wrap_screen_type = R.style.Theme_Light_NoTitleBar_Fullscreen;
            }

            final Dialog dialog = new Dialog(act, full_wrap_screen_type);
            dialog.setContentView(relativeLayout);
            okBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //To change body of implemented methods use File | Settings | File Templates.
                    dialog.dismiss();
                }
            });
            dialog.show();

        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}

对话框只有背景图片,和一个按钮。Drawable文件夹下放两张图片,enjoyit_help_img.png 帮助的背景图片,enjoyit_ok_img.png是确认按钮图片。调用对话框方法时,设置确认按钮在底部居中:

        DialogUtil.showDrawableDialog((Activity) this,
                DialogUtil.Screen.full_screen,
                "enjoyit_help_img",
                "enjoyit_ok_img",
                new DialogUtil.EGravityType[]{
                        DialogUtil.EGravityType.BOTTOM, DialogUtil.EGravityType.CENTER_HORIZONTAL
                });



引用方式如下:

你可能感兴趣的:(android自定义对话框)