一个简单的自定义Dialog
2016-09-21 15:28
387 查看
尊重他人的劳动成果,转载请标明出处:http://blog.csdn.net/gengqiquan/article/details/52608636, 本文出自:【gengqiquan的博客】
项目中会有各种各样的dialog弹窗,每个都拿布局去写很烦,以前写过一个多配置的dialog框架,没带到新公司,没事的时候写了一个简洁版的。
支持有无标题,支持替换内容View。支持只显示确定按钮,支持修改按钮文字颜色。支持普通的点击事件,支持背景调暗,总之是一些基本的dialog功能
随便贴两张图
带标题的
![](http://img.blog.csdn.net/20160921151328422)
调用代码
不带标题
![](http://img.blog.csdn.net/20160921151146706)
调用代码
代码不怎么多,虽然简单,但还是可配置的,builder模式,具体的功能方法看builder类
dialog构建类类
布局
样式
只是个基础的架子,之所以没有像以前那样写一个完整的框架,是觉得不同的项目要求不一样,为一个dialog写太多、太全只是白白占用方法数而已,有什么特殊需求的方法大家可以自己加
我建了一个QQ群(群号:121606151),用于大家讨论交流Android技术问题,有兴趣的可以加下,大家一起进步。
项目中会有各种各样的dialog弹窗,每个都拿布局去写很烦,以前写过一个多配置的dialog框架,没带到新公司,没事的时候写了一个简洁版的。
支持有无标题,支持替换内容View。支持只显示确定按钮,支持修改按钮文字颜色。支持普通的点击事件,支持背景调暗,总之是一些基本的dialog功能
随便贴两张图
带标题的
调用代码
new Builder(mContext) .title("300速贷") .message("联系车300,业务员立即上门办理贷款业务") .SureText("拨号") .sureTextColor(R.color.blue) .setSureOnClickListener((view) -> { phone(mBean.getPhone()); }) .builder() .show();
不带标题
调用代码
new Builder(mContext) .noTitle() .message("是否删除此条订阅条件") .SureText("是") .setSureOnClickListener((view) -> { delete(item.getId()); }) .builder() .show();
代码不怎么多,虽然简单,但还是可配置的,builder模式,具体的功能方法看builder类
dialog构建类类
package com.sunshine.dialglibrary; import android.app.ActionBar; import android.app.Dialog; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; /** * Created by 耿 on 2016/9/11. */ public class Builder { private Context context; private View mLayout; private TextView sureButton; private TextView cancleButton; private boolean mCancelable = false; private View.OnClickListener sureClickListener, cancelClickListener; Dialog dialog; public Builder(Context context) { this.context = context; LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); mLayout = inflater.inflate(R.layout.base_dialog_layout, null); } //能否返回键取消 public Builder setCancelable(Boolean boolean1) { this.mCancelable = boolean1; return this; } public Builder title(int title) { ((TextView) mLayout.findViewById(R.id.title)).setText(title); return this; } public Builder title(String title) { ((TextView) mLayout.findViewById(R.id.title)).setText(title); return this; } public Builder titleColor(int color) { ((TextView) mLayout.findViewById(R.id.title)).setTextColor(context.getResources().getColor(color)); return this; } public Builder titleBackColor(int color) { mLayout.findViewById(R.id.title_back).setBackgroundColor(context.getResources().getColor(color)); return this; } public Builder setSureOnClickListener(View.OnClickListener listener) { this.sureClickListener = listener; return this; } public Builder message(String message) { ((TextView) mLayout.findViewById(R.id.message)).setText(message); return this; } public Builder message(int message) { ((TextView) mLayout.findViewById(R.id.message)).setText(message); return this; } public Builder sureTextColor(int color) { ((TextView) mLayout.findViewById(R.id.sure)).setTextColor(context.getResources().getColor(color)); return this; } public Builder cancelTextColor(int color) { ((TextView) mLayout.findViewById(R.id.cancel)).setTextColor(context.getResources().getColor(color)); return this; } //切换内容view public Builder setView(View v) { ((LinearLayout) mLayout.findViewById(R.id.content)).removeAllViews(); ((LinearLayout) mLayout.findViewById(R.id.content)).addView(v); return this; } //确定按钮文本 public Builder SureText(String str) { ((TextView) mLayout.findViewById(R.id.sure)).setText(str); return this; } public Builder setView(View v, LinearLayout.LayoutParams params) { ((LinearLayout) mLayout.findViewById(R.id.content)).removeAllViews(); ((LinearLayout) mLayout.findViewById(R.id.content)).addView(v, params); return this; } public Builder showCancelButton(boolean isShow) { if (!isShow) mLayout.findViewById(R.id.cancel).setVisibility(View.GONE); return this; } public Builder noTitle() { mLayout.findViewById(R.id.title_back).setVisibility(View.GONE); return this; } public Builder justMessageDialog() { sureClickListener = new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }; return this; } public Dialog builder() { dialog = new Dialog(context, R.style.base_dialog); dialog.setCancelable(mCancelable); dialog.addContentView(mLayout, new ActionBar.LayoutParams( ActionBar.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.WRAP_CONTENT)); if (sureClickListener != null) { mLayout.findViewById(R.id.sure).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sureClickListener.onClick(v); dialog.dismiss(); } }); } mLayout.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (cancelClickListener != null) { cancelClickListener.onClick(v); } dialog.dismiss(); } }); return dialog; } }
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dialoglayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/title_back" android:gravity="center" android:minHeight="45dip"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:gravity="center" android:maxLines="2" android:ellipsize="end" android:layout_marginRight="15dp" android:text="标题" android:textColor="@color/gray333333" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:id="@+id/content" android:layout_width="match_parent" android:gravity="center_horizontal" android:layout_height="wrap_content" android:minHeight="80dip"> <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="15dip" android:text="正文" android:textColor="@color/gray333333" android:textSize="18sp" /> </LinearLayout> <View android:id="@+id/line2" android:layout_width="fill_parent" android:layout_height="0.5dip" android:background="@color/line" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingRight="8.0dip"> <TextView android:id="@+id/cancel" android:layout_width="0dip" android:layout_height="40dp" android:layout_marginLeft="8.0dip" android:layout_weight="1.0" android:gravity="center" android:paddingBottom="5dip" android:paddingTop="5dip" android:text="取消" android:textColor="@color/gray333333" android:textSize="16sp" /> <View android:layout_width="0.5dp" android:layout_height="match_parent" android:background="@color/line" /> <TextView android:id="@+id/sure" android:layout_width="0dip" android:layout_height="40dp" android:layout_marginLeft="8.0dip" android:layout_weight="1.0" android:gravity="center" android:paddingBottom="5dip" android:paddingTop="5dip" android:text="确认" android:textColor="@color/yellowff6600" android:textSize="16sp" /> </LinearLayout> </LinearLayout>
样式
-<style parent="@android:style/Theme.Dialog" name="base_dialog"> <item name="android:windowFrame">@null</item> <!-- 边框 --> <item name="android:windowIsFloating">true</item> <!-- 是否浮现在activity之上 --> <item name="android:windowIsTranslucent">true</item> <!-- 半透明 --> <item name="android:windowNoTitle">true</item> <!-- 无标题 --> <item name="android:windowBackground">@android:color/black</item> <!-- 背景透明 --> <item name="android:backgroundDimEnabled">true</item> <!-- 模糊 --> </style>
只是个基础的架子,之所以没有像以前那样写一个完整的框架,是觉得不同的项目要求不一样,为一个dialog写太多、太全只是白白占用方法数而已,有什么特殊需求的方法大家可以自己加
我建了一个QQ群(群号:121606151),用于大家讨论交流Android技术问题,有兴趣的可以加下,大家一起进步。
相关文章推荐
- 实现一个简单的自定义Dialog弹窗。
- 一个简单的自定义Dialog
- 简单的自定义一个拥有圆角的Dialog
- 一个简单的带Body的自定义Tag开发过程
- 一个简单的自定义通信协议(socket)
- 在ArcGIS Server Java ADF中创建一个自定义task的简单步骤
- 一个简单的自定义jsp标签
- 一个简单的自定义ClassLoader的实现
- Groovy探索 自定义Range 一 一个简单的自定义Range类
- Feature开发一(制作一个简单的自定义菜单)
- 一个简单的带Body的自定义Tag开发过程
- 一个简单的分页控件,用来自定义分页用的
- 一个简单自定义屏幕,并调用word和excel的样例程序 1000弹出选择按钮
- 简单的一个自定义分页(C#)
- 一个简单自定义屏幕,并调用word和excel的样例程序
- .NET自定义异常的一个简单应用(转载)
- Groovy探索 自定义Range 一 一个简单的自定义Range类
- .NET自定义异常的一个简单应用
- 一个简单的自定义程序日志小样例
- 一个简单的MenuStrip的自定义Renderer