自定义dialog,非常方便复用
来源:互联网 发布:自定义域名邮箱 编辑:程序博客网 时间:2024/06/10 17:30
自定义的这个dialog的使用方法:
// testCustomDialog.Builder customBuilder = new CustomDialog.Builder(this);customBuilder.setTitle("标题").setMessage("message").setTitleImage(R.drawable.ic_launcher).setNegativeButton("取消", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {dialog.dismiss();}}).setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});CustomDialog dialog = customBuilder.create();dialog.show();
如上.效果图如下
当然这是可以定制的,如果你想改变dialog布局也是相当容易.如果想要达到上面的效果需要一些资源,最下面会提供.
在使用这自定义的dialog之前,我们先要将dialog自定义出来,如下
/** * * Create custom Dialog windows for your application Custom dialogs rely on * custom layouts wich allow you to create and use your own look & feel. * * Under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html * * <a href="http://my.oschina.net/arthor" target="_blank" * rel="nofollow">@author</a> antoine vianey * */public class CustomDialog extends Dialog {public CustomDialog(Context context, int theme) {super(context, theme);}public CustomDialog(Context context) {super(context);}/** * Helper class for creating a custom dialog */public static class Builder {private Context context;private String title;private String message;private String positiveButtonText;private String negativeButtonText;private View contentView;// add by mio to set TitleImageprivate int resid;private DialogInterface.OnClickListener positiveButtonClickListener,negativeButtonClickListener;public Builder(Context context) {this.context = context;}/** * Set the Dialog TitleImage from String * * @author mio * @param id * @return */public Builder setTitleImage(int resid) {this.resid = resid;return this;}/** * Set the Dialog message from String * * @param title * @return */public Builder setMessage(String message) {this.message = message;return this;}/** * Set the Dialog message from resource * * @param title * @return */public Builder setMessage(int message) {this.message = (String) context.getText(message);return this;}/** * Set the Dialog title from resource * * @param title * @return */public Builder setTitle(int title) {this.title = (String) context.getText(title);return this;}/** * Set the Dialog title from String * * @param title * @return */public Builder setTitle(String title) {this.title = title;return this;}/** * Set a custom content view for the Dialog. If a message is set, the * contentView is not added to the Dialog... * * @param v * @return */public Builder setContentView(View v) {this.contentView = v;return this;}/** * Set the positive button resource and it's listener * * @param positiveButtonText * @param listener * @return */public Builder setPositiveButton(int positiveButtonText,DialogInterface.OnClickListener listener) {this.positiveButtonText = (String) context.getText(positiveButtonText);this.positiveButtonClickListener = listener;return this;}/** * Set the positive button text and it's listener * * @param positiveButtonText * @param listener * @return */public Builder setPositiveButton(String positiveButtonText,DialogInterface.OnClickListener listener) {this.positiveButtonText = positiveButtonText;this.positiveButtonClickListener = listener;return this;}/** * Set the negative button resource and it's listener * * @param negativeButtonText * @param listener * @return */public Builder setNegativeButton(int negativeButtonText,DialogInterface.OnClickListener listener) {this.negativeButtonText = (String) context.getText(negativeButtonText);this.negativeButtonClickListener = listener;return this;}/** * Set the negative button text and it's listener * * @param negativeButtonText * @param listener * @return */public Builder setNegativeButton(String negativeButtonText,DialogInterface.OnClickListener listener) {this.negativeButtonText = negativeButtonText;this.negativeButtonClickListener = listener;return this;}/** * Create the custom dialog * ================need some resource=================== */public CustomDialog create() {LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);// instantiate the dialog with the custom Themefinal CustomDialog dialog = new CustomDialog(context,R.style.Dialog);dialog.setCanceledOnTouchOutside(false);//load dialog layout,you can diy this one! View layout = inflater.inflate(R.layout.custom_dialog_layout, null);dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));// set the dialog titleImage// add by mio((ImageView) layout.findViewById(R.id.title_image)).setBackgroundResource(resid);// set the dialog title((TextView) layout.findViewById(R.id.title)).setText(title);// set the confirm buttonif (positiveButtonText != null) {((Button) layout.findViewById(R.id.positiveButton)).setText(positiveButtonText);if (positiveButtonClickListener != null) {((Button) layout.findViewById(R.id.positiveButton)).setOnClickListener(new View.OnClickListener() {public void onClick(View v) {positiveButtonClickListener.onClick(dialog,DialogInterface.BUTTON_POSITIVE);}});}} else {// if no confirm button just set the visibility to GONElayout.findViewById(R.id.positiveButton).setVisibility(View.GONE);}// set the cancel buttonif (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(dialog,DialogInterface.BUTTON_NEGATIVE);}});}} else {// if no confirm button just set the visibility to GONElayout.findViewById(R.id.negativeButton).setVisibility(View.GONE);}// set the content messageif (message != null) {((TextView) layout.findViewById(R.id.message)).setText(message);} else if (contentView != null) {// if no message set// add the contentView to the dialog body((LinearLayout) layout.findViewById(R.id.content)).removeAllViews();((LinearLayout) layout.findViewById(R.id.content)).addView(contentView, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));}dialog.setContentView(layout);return dialog;}}}
以上就是自定义dialog的代码,其中create方法是可以根据需求自定义的,
简单的解析一下create方法吧.
// instantiate the dialog with the custom Themefinal CustomDialog dialog = new CustomDialog(context,R.style.Dialog);构造一个dialog,第二个参数是一个dialog的主题,在valuses的style中,代码如下
关于下面item的解释可以参考http://blog.csdn.net/dyllove98/article/details/8841602
<style name="Dialog" parent="android:style/Theme.Dialog"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:backgroundDimEnabled">true</item> </style>
dialog.setCanceledOnTouchOutside(false);
//load dialog layout,you can diy this one! View layout = inflater.inflate(R.layout.custom_dialog_layout, null);dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));这里主要是将dialog给inflate出来,如果要改变dialog外观,可以修改R.layout.custom_dialog_layout的布局文件,里面的id不建议改动,否则oncreate的其他代码都要变动.
当然R.layout.custom_dialog_layout的布局文件下面也会给出.
这里比如我不想要dialog的标题栏,可以在R.layout.custom_dialog_layout的布局文件中改动一下如下:
将上面的都删掉即可,但是注意oncreate关于上面的id都要删掉,即下面的选中的区域都要删除.
以上就是自定义dialog的用法,
资源文件地址,在下面的Demo工程中
点击打开链接
1 0
- 自定义dialog,非常方便复用
- android方便的自定义Dialog工厂类
- 各种Popwindow和Dialog的汇总以及仿ios的,可以自定义布局,也可以傻瓜式的代码生成,使用非常方便
- Android高拓展性,更加方便的自定义Dialog.
- 非常简洁的自定义dialog实现方式(带源码)
- 非常方便的CCMenuItemSprite
- Android 自定义ImageView支持缩放,拖拽,方便复用
- Android 自定义ImageView支持缩放,拖拽,方便复用
- Android 自定义ImageView支持缩放,拖拽,方便复用
- Android 自定义ImageView支持缩放,拖拽,方便复用
- android自定义(无埋点)收集用户行为统计 (非常方便 可以自己拓展)
- 用Dialog实现自定义对话框
- 自定义Dialog---继承Dialog
- 自定义Dialog android Dialog
- 非常方便的d3d截屏~!
- cocos2dx非常方便的宏
- MyEclipse非常方便的快捷键
- MyEclipse非常方便的快捷键
- Qual F&Q
- UVa-10815-Andy's First Dictionary
- C++primer学习笔记《4》
- 数据结构之二叉搜索树
- CentOS 6.3的U盘安装方法
- 自定义dialog,非常方便复用
- ACM书籍推荐
- CentOS - yum Multilib Version Problems Found
- Hibernate通过SQL查询常量时只能返回第一个字符的解决方法
- c u b e m x E t h P t p 如 何 激活
- UVA - 11300 Spreading the Wealth
- 【易择微信系列6】晃动的密码
- Activity数据通信
- 【HDU】2433 Travel 最短路树优化