对话框Dialog使用总结
来源:互联网 发布:知轩藏书进不去 编辑:程序博客网 时间:2024/06/05 19:31
在项目中,我们经常用到对话框,这里我直接介绍AlertDialog的基础类Dialog,因为在我们定制化对话框都是Dialog的。
(1) Dialog的创建
Dialog dlgShare = new Dialog(this);
dlgShare.show();
如果就如上仅仅new一个Dialog这样显示出的dialog是没有任何内容的。
(2) 设置Dialog的标题
dlgShare.setTitle("Test");
如果仅仅只设置了标题而没有设置内容那么对话框只显示个标题,如下:
(3) 设置对话框的内容
Dialog内容设置主要通过以下两个方法:
public void setContentView(int layoutResID) public void setContentView(View view)
Eg:
Dialog dlgShare = new Dialog(this);dlgShare.setContentView(R.layout.dialog_share);dlgShare.setTitle("Test");
dialog_share.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:background="#FFFFFF" android:orientation="vertical" > <ImageView android:layout_width="fill_parent" android:layout_height="2dp" android:background="#249EF6" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="50dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="分享到" android:textColor="#249EF6" /> </RelativeLayout> <ImageView android:layout_width="fill_parent" android:layout_height="1dp" android:background="#E6E6E6" /> <GridView android:layout_width="fill_parent" android:layout_height="180dp" android:numColumns="3" android:stretchMode="columnWidth" > </GridView> <Button android:layout_width="fill_parent" android:layout_height="50dp" android:layout_margin="15dp" android:background="#F5F5F5" android:text="取消" android:textColor="#848484" /></LinearLayout>
实现效果如下:
(4) 取消dialog的标题栏
Dialog标题默认是可以设置的,Android中取消自定义dialog的标题栏, 只需在
dialog.setContentView(R.layout.popwin_chooseversion);
前面加一句:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
(5) Dialog的布局结构
如果使用了
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().getDecorView()内部结构如下:
PhoneWindow$DecorView包含了一个子View(FrameLayout)2,
该子View(FrameLayout)2包含了两个子View,一个是ViewStub 3,
一个是FrameLayout4, 而FrameLayout 4 包含的内容对应的就是
Dialog.setContentView的内容。
这时通过Dialog.findViewById(android.R.id.title)和window. findViewById(android.R.id.title) 是得不到title的
如果没有设置Window.FEATURE_NO_TITLE,那么默认是有标题栏的,dialog.getWindow().getDecorView()内部结构如下:
PhoneWindow$DeocrView包含一个子LinearLayout 2, 此LinearLayt,时垂直布局,它包含两子view,一个是TextView 3 ,一个是FrameLayout4
TextView 对应dialog的标题,对应dialog.setTitle(…)方法设置的,
而FrameLayout包含了一个子FrameLayout,该子FrameLayout存放的是对应dialog的内容,对应dialog.setContentView(view)方法设置的。
这时通过Dialog.findViewById(android.R.id.title)和window. findViewById(android.R.id.title) 可以得到标题对应的TextView
(6) 设置标题的颜色,标题栏的背景颜色
知道了Dialog的布局结构那我们很容易设置标题的颜色。
先获取title的TextView
Dialog.findViewById(android.R.id.title)其内部是调用Dialog.getWindow().findViewById(android.R.id.title),View bv = dialog.findViewById(android.R.id.title );((TextView) bv).setTextColor(Color.WHITE);((View) bv.getParent()).setBackgroundColor(Color.RED);
这样,即可设置标题栏背景为红色,字体为白色
(7) 对话框背景设置和清除
清除对话框的模糊背景
win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
设置对话框的模糊背景
win.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,WindowManager.LayoutParams.FLAG_DIM_BEHIND);
或
win.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
对话框背景默认是模糊的,默认WindowManager.LayoutParams.FLAG_DIM_BEHIND
(8) 对话框位置以及全屏的设置
Eg:设置对话框位于屏幕底部,并且水平铺满
Dialog dlgShare = new Dialog(this)...
Window win = dlgShare.getWindow();win.setGravity(Gravity.BOTTOM);//对话框底部显示,如果没设置默认是居中显示的
</pre><pre name="code" class="java"><strong>水平方向的全屏</strong>
</pre><p></p><pre name="code" class="java">(a)样式+代码<pre name="code" class="html"><style name="info_dialog" parent="android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item></style>dialog = new Dialog(this,R.style.info_dialog);Window win = dialog.getWindow();WindowManager.LayoutParams lp = win.getAttributes(); lp.width = WindowManager.LayoutParams.FILL_PARENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; win.setAttributes(lp);
(b)纯代码设置Dialog dialog = new Dialog(this);Window win = dialog.getWindow();win.getDecorView().setPadding(0, 0, 0, 0);//间距为0WindowManager.LayoutParams lp = win.getAttributes();lp.width = WindowManager.LayoutParams.FILL_PARENT;lp.height = WindowManager.LayoutParams.WRAP_CONTENT;win.setAttributes(lp);
由于对话框的样式文件内部有pading属性,所以如果没有
win.getDecorView().setPadding(0,0, 0, 0)这一行代码,
不管我们怎样设置lp.width,lp.x,lp.y对话框水平方向是占不满的,左右始终是有间距的。
全屏必须设置dialog的window属性layoutparams,设置pading left=0 right=0,如果dialog样式文件有
<pre name="code" class="html"><item name="android:windowFrame">@null</item>那么相当于没有了pading想直接通过样式来设置dialog全屏是不行的。
Eg:
<style name="info_dialog" parent="android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFullscreen">true</item> <item name="android:windowIsFloating">true</item> </style>
如上即使有 android:windowFullscreen 属性dialog也不能全屏。
(9) 对话框黑色边框的解决
(a)代码解决Dialog dialog = new Dialog(this);Window win = dialog.getWindow();win.setBackgroundDrawableResource(android.R.color.transparent);(b) 样式+代码定义dialog的样式文件:<pre name="code" class="html"><style name="info_dialog" parent="android:style/Theme.Dialog"><item name="android:windowBackground">@android:color/transparent</item></style>Dialog dialog = new Dialog(this,R.style.info_dialog);
- 对话框Dialog使用总结
- 对话框Dialog使用总结
- 对话框Dialog总结
- 对话框Dialog总结
- Dialog 对话框总结
- 对话框Dialog的使用
- Dialog对话框的使用
- Dialog(对话框)的使用
- Android Dialog,DialogFragment对话框总结
- android Dialog对话框使用示例
- 对话框Dialog控件的使用
- 对话框Dialog控件的使用
- android 对话框(Dialog)使用
- android dialog使用总结
- Dialog使用总结
- Dialog使用个人总结
- Dialog(一)——对话框(Dialog)基本使用
- Android中对话框(dialog)的使用
- Jump Game II
- Permission Denial not exported from uid
- 一个IT男的自白
- OSI模型和TCP/IP模型
- 删除.note.gnu.build-id段号
- 对话框Dialog使用总结
- poi 导入excel spring mvc
- Endophytic Shiitake Mushrooms deserve their own class due to their behavior
- Java-Intersection of Two Linked Lists
- 安装LR时报未找到putty.GID
- Ng机器学习系列补充:2、分类和回归树算法CART
- 【云简评】之七《Forrester 2015年10大云预测》
- 手游团队如何提高生存能力
- Java数组操作的10大方法