【Android学习笔记】自定义Dialog
来源:互联网 发布:linux 虚拟化技术 编辑:程序博客网 时间:2024/05/25 08:12
概述
Android为我们提供了丰富的对话框。实际开发中我们根据需求,往往需要自定义一些好看的Dialog,下面对以三种创建对话框的方式为例,简单对比了他们之间的优缺点。
实现
dialog_edit.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="wrap_content" android:background="@drawable/dialog_bg" android:orientation="vertical" > <TextView android:id="@+id/tv_dialog_set_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:gravity="center" android:text="标题" android:textColor="#000000" android:textSize="20sp" /> <EditText android:layout_margin="5dp" android:id="@+id/et_dialog_set_oldpsw" style="@style/NewEditText" android:focusable="true" android:focusableInTouchMode="true" android:inputType="textEmailAddress" android:hint="输入密码" /> <View android:layout_width="match_parent" android:layout_height="1sp" android:background="@color/dialog_line" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:padding="20dp" android:id="@+id/btn_dialog_set_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="left" android:layout_weight="1" android:background="@null" android:text="取消" /> <View android:layout_width="1sp" android:layout_height="match_parent" android:layout_gravity="center" android:background="@color/dialog_line" /> <Button android:padding="20dp" android:id="@+id/btn_dialog_set_ok" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_weight="1" android:background="@null" android:text="确定" /> </LinearLayout></LinearLayout>
方法一:通过AlertDialog的setView方法来创建
AlertDialog dialog = new Builder(context).create();View view = LayoutInflater.from(context).inflate(R.layout.dialog_edit, null);dialog.setView(view,0,0,0,0);dialog.show();
效果展示:
缺点:对话框原有的背景无法去掉,如果你把对话框弄成如图这样圆角的,那么背景会漏出来,改用直角才能把它遮住
方法二:通过AlertDialog的getWindow来创建
AlertDialog dialog = new Builder(context).create();dialog.show();//这里得放在dialog.show后面Window window = dialog.getWindow();window.setGravity(Gravity.CENTER_HORIZONTAL);WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);DisplayMetrics outMetrics = new DisplayMetrics();wm.getDefaultDisplay().getMetrics(outMetrics);WindowManager.LayoutParams lp = window.getAttributes();lp.width = (int) (outMetrics.widthPixels * 0.9);window.setAttributes(lp);window.setContentView(R.layout.dialog_edit);
效果展示:
优点:因为是通过Window这个对象来创建的,所以Dialog原有的背景没了,这样就可以定义出各种样式比较美观的Dialog
缺点:代码稍微有点复杂
存在问题:乍一看似乎是没问题,但是如果你的布局文件中有EditText这个组件就不行了,你会发现软键盘怎么都弹不出来,我尝试过网上的各种方法,最终都没能解决这个问题、希望知道解决办法的大神能告诉小弟。
方法三:通过Dialog来创建
首先需要在styles.xml文件中配置一个style
<style name="dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">false</item> <item name="android:windowNoTitle">true</item> <item name="android:background">@null</item> <item name="android:backgroundDimEnabled">false</item> <item name="android:windowBackground"> @android:color/transparent </item></style>其次在代码中应用这个style
Dialog dialog = new Dialog(context, R.style.dialog);View view = LayoutInflater.from(context).inflate(R.layout.dialog_edit, null);WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);DisplayMetrics outMetrics = new DisplayMetrics();wm.getDefaultDisplay().getMetrics(outMetrics);ViewGroup.LayoutParams lp = new LayoutParams((int) (outMetrics.widthPixels * 0.9), LayoutParams.WRAP_CONTENT);dialog.setContentView(view, lp);dialog.show();效果展示:
优点:以此种方式创建的Dialog,美观方面方面自然不用说,关键是它解决了上一种创建方式不能弹出软键盘的问题。
缺点:代码两稍多、别的暂时还没发现有什么问题
总结
总的来说,三种创建方式各有所长,在实际开发中按需使用就行了
1 0
- 【Android学习笔记】自定义Dialog
- 学习笔记之自定义Dialog
- Android学习笔记: Dialog
- android学习之-自定义Dialog
- android学习之自定义dialog
- 自定义Dialog android Dialog
- 学习笔记——自定义Dialog
- android学习记录1:android自定义Dialog
- Android学习——自定义Dialog
- Android学习笔记之为Dialog自定义布局,并说明空指针问题
- android应用开发揭秘学习笔记(Dialog对话框-例2:用xml自定义对话框并调用)
- android开发学习笔记——自定义布局的dialog宽度的问题
- 【Android】Dialog以及自定义Dialog
- android 系统Dialog,自定义Dialog
- 自定义Dialog(笔记)
- android学习笔记-dialog的使用
- Android开发学习笔记之Dialog
- android 自定义dialog 自定义dialog 宽度问题
- GTK+ Widget的内部结构与工作流程
- js document
- jQuery扩展 插件弹出层
- Android之Spinner的功能和用法
- 看google怎样将android回归在自己的保护伞之下
- 【Android学习笔记】自定义Dialog
- 基于sparksql调用shell脚本执行SQL
- 电子邮件营销怎么做,点击打开效果才好?
- WPF 创建二维码
- 共享库(.so文件)
- [error handle][mahout] 由于空字符串错误导致model创建失败
- Android Studio关联SVN相关操作
- 5.1信号一2015/7/28
- ERP兵法——从案例透视方法(实施篇上)