Android 自定义Dialog的简单实现

来源:互联网 发布:解压war linux tar 编辑:程序博客网 时间:2024/05/20 15:40

感觉好像已经很久没更新博客了,前段时间主要是忙于新工作的事情,现在我又可以抽出时间来写写博客了,今天分享一篇自定义Dialog的简单实现

一、前言
现在很多App的提示对话框都非常个性化,我们早已不再满足于使用系统的对话框样式,一般而言,我们会根据自家的App的主题,设计出相对应风格的Dialog,今天我就给大家分享一篇自定义的Dialog,主要的功能有:

  1. 设置标题以及标题字体的颜色
  2. 设置内容以及内容字体的颜色
  3. 设置确认按钮的文字以及字体颜色
  4. 设置取消按钮的文字以及字体颜色
  5. 设置取消按钮和确定按钮是否可见等
    和往常一样,我们先来看看效果图
    这里写图片描述

二、现在我们来简单看下项目结构图
这里写图片描述
核心文件是CustomDialog.java和styles主题样式,其它都是次要的。项目中我们只需要将CustomDialog文件和对应的xml布局widget_dialog以及drawable文件夹下的文件复制到自己的工程项目即可使用。

三、自定义CustomDialog的简单调用,很简单的几句代码,还有其他的方法调用,可以看CustomDialog的代码啦

  CustomDialog dialog = new CustomDialog(this, new View.OnClickListener() {            @Override            public void onClick(View arg0) {                switch (arg0.getId()) {                    case R.id.dialog_btn_sure:                        Toast.makeText(mContext, "重新拍照", Toast.LENGTH_SHORT).show();                        break;                }            }        });        dialog.setMessage("您确定要重新拍照吗?");        dialog.setTitle("温馨提示");        dialog.setBtnSure("重新拍照");        dialog.setBtnCancel("算了吧");        dialog.setBtnSureColor(mContext.getResources().getColor(                R.color.green));        dialog.show();

四、现在就让我们来看看CustomDialog具体实现吧,代码写得很详细,这里我把代码贴出来,就不一一解释啦
(1)CustomDialog.java

package per.lijuan.customdialogdome;import android.app.Dialog;import android.content.Context;import android.view.Gravity;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.Button;import android.widget.LinearLayout;import android.widget.TextView;/** * @author: lijuan * @description: 自定义提示dialog * @date: 2016-11-29 * @time: 15:05 */public class CustomDialog extends Dialog {    private TextView mTvMessage, mTvTitle;    private Button mBtnCancel, mBtnSure;    private LinearLayout mLlTitle, mLlContent;    private Context mContext;    private View.OnClickListener mListener;    public CustomDialog(Context context, View.OnClickListener listener) {        super(context, R.style.Alert_Dialog_Style);        mListener = listener;        mContext = context;        init(context);    }    private void init(Context context) {        View view = View.inflate(context, R.layout.widget_dialog, null);        setContentView(view);        init(view);    }    private void init(View v) {        mLlTitle = (LinearLayout) v.findViewById(R.id.ll_title);        mLlTitle.setVisibility(View.GONE);        mLlContent = (LinearLayout) v.findViewById(R.id.ll_content);        mTvTitle = (TextView) v.findViewById(R.id.tv_title);        mTvMessage = (TextView) v.findViewById(R.id.tv_message);        mBtnCancel = (Button) v.findViewById(R.id.dialog_btn_cancel);        mBtnSure = (Button) v.findViewById(R.id.dialog_btn_sure);        mBtnSure.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View arg0) {                dismiss();                if (mListener != null)                    mListener.onClick(arg0);            }        });        mBtnCancel.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View arg0) {                dismiss();                if (mListener != null)                    mListener.onClick(arg0);            }        });        Window window = getWindow();        WindowManager.LayoutParams params = window.getAttributes();        params.gravity = Gravity.CENTER;        params.alpha = 1.0f;        params.width = ScreenUtil.getScreenWidth(mContext);        window.setAttributes(params);    }    /**     * 设置标题     *     * @param title     */    public void setTitle(String title) {        mLlTitle.setVisibility(View.VISIBLE);        mTvTitle.setText(title);        mLlTitle.setBackgroundColor(mContext.getResources().getColor(                R.color.green));        mLlContent.setBackgroundColor(mContext.getResources().getColor(                R.color.white));    }    /**     * 设置内容     *     * @param message     */    public void setMessage(String message) {        mTvMessage.setText(message);    }    /**     * 设置确认按钮的文字     *     * @param sure     */    public void setBtnSure(String sure) {        mBtnSure.setText(sure);    }    /**     * 设置取消按钮的文字     *     * @param cancel     */    public void setBtnCancel(String cancel) {        mBtnCancel.setText(cancel);    }    /**     * 设置取消按钮的文字     *     * @param visible     */    public void setBtnCancelVisible(int visible) {        mBtnCancel.setVisibility(visible);        if (visible == View.GONE) {            mBtnSure.setBackgroundResource(R.drawable.selector_ontouch);        } else {            mBtnSure.setBackgroundResource(R.drawable.selector_bottom_right_corner);        }    }    /**     * 设置标题的颜色     *     * @param color     */    public void setTitleColor(int color) {        mTvTitle.setTextColor(color);    }    /**     * 设置内容的颜色     *     * @param color     */    public void setMessageColor(int color) {        mTvMessage.setTextColor(color);    }    /**     * 确认按钮的颜色     *     * @param color     */    public void setBtnSureColor(int color) {        mBtnSure.setTextColor(color);    }    /**     * 确认取消的颜色     *     * @param color     */    public void setBtnCalcelColor(int color) {        mBtnCancel.setTextColor(color);    }    /**     * 设置自定义的view     *     * @param contentView     */    public void setCustomContentView(View contentView) {        mLlContent.removeAllViews();        mLlContent.addView(contentView);    }    /**     * 设置确认是否有效     *     * @param enabled     */    public void setBtnSureEnable(boolean enabled) {        mBtnSure.setEnabled(enabled);        if (!enabled) {            mBtnSure.setTextColor(mContext.getResources().getColor(                    R.color.black));        }    }    /**     * 设置取消是否有效     *     * @param enabled     */    public void setBtnCancelEnable(boolean enabled) {        mBtnCancel.setEnabled(enabled);        if (!enabled) {            mBtnCancel.setTextColor(mContext.getResources().getColor(                    R.color.black));        }    }}

(2)定义styles主题样式

<!-- 自定义的AlertDialog的样式 -->    <style name="Alert_Dialog_Style" parent="@android:style/Theme.Dialog">        <item name="android:windowBackground">@android:color/transparent</item>        <item name="android:windowContentOverlay">@null</item>        <item name="android:windowIsFloating">true</item>        <item name="android:windowFrame">@null</item>        <item name="android:backgroundDimEnabled">true</item>        <item name="android:windowNoTitle">true</item>        <item name="android:windowIsTranslucent">true</item>    </style>

(3)布局文件widget_dialog.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"    android:paddingLeft="30dp"    android:paddingRight="30dp">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <LinearLayout            android:id="@+id/ll_title"            android:layout_width="match_parent"            android:layout_height="50dp"            android:background="@color/green"            android:orientation="vertical">            <TextView                android:id="@+id/tv_title"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:gravity="center|left"                android:minHeight="50dp"                android:padding="10dp"                android:textColor="@color/white"                android:textSize="18sp" />        </LinearLayout>        <ScrollView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:overScrollMode="never">            <LinearLayout                android:id="@+id/ll_content"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:background="@color/white"                android:minHeight="60dp"                android:orientation="vertical"                android:paddingBottom="25dp"                android:paddingLeft="15dp"                android:paddingRight="15dp"                android:paddingTop="25dp">                <TextView                    android:id="@+id/tv_message"                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:layout_gravity="center"                    android:gravity="center"                    android:textColor="@color/black"                    android:textSize="18sp" />            </LinearLayout>        </ScrollView>        <View            android:layout_width="match_parent"            android:layout_height="1px"            android:background="@color/gray" />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="50dp"            android:orientation="horizontal">            <Button                android:id="@+id/dialog_btn_cancel"                android:layout_width="0dp"                android:layout_height="50dp"                android:layout_weight="1"                android:background="@drawable/selector_bottom_left_corner"                android:gravity="center"                android:text="@string/common_text_cancel"                android:textColor="@color/black"                android:textSize="18sp" />            <View                android:layout_width="1px"                android:layout_height="25dp"                android:layout_gravity="center"                android:background="@color/gray" />            <Button                android:id="@+id/dialog_btn_sure"                android:layout_width="0dp"                android:layout_height="50dp"                android:layout_weight="1"                android:background="@drawable/selector_bottom_right_corner"                android:gravity="center"                android:text="@string/common_text_sure"                android:textColor="@color/black"                android:textSize="18sp" />        </LinearLayout>    </LinearLayout></LinearLayout>

(4)其中还涉及到屏幕分辨率工具类ScreenUtil,这里就不贴出来啦,还记得我之前写过的一篇Android 屏幕分辨率工具类使用,有兴趣的可以看看
(5)selector_bottom_left_corner.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true">        <shape>            <solid android:color="@color/gray" />            <corners android:bottomLeftRadius="5dp" />        </shape>    </item>    <item android:state_pressed="false">        <shape>            <solid android:color="@color/white" />            <corners android:bottomLeftRadius="5dp" />        </shape>    </item></selector>

(6)selector_bottom_right_corner.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true">        <shape>            <solid android:color="@color/gray" />            <corners android:bottomRightRadius="5dp" />        </shape>    </item>    <item android:state_pressed="false">        <shape>            <solid android:color="@color/white" />            <corners android:bottomRightRadius="5dp" />        </shape>    </item></selector>

(7)selector_ontouch.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true">        <shape>            <solid android:color="@color/gray" />        </shape>    </item>    <item android:state_pressed="false">        <shape>            <solid android:color="@color/white" />        </shape>    </item></selector>

好了,本篇文章已经全部写完了,存在总结不到位的地方还望指导,感谢^_^
源码下载

2 0