Android中自定义对话框(Dialog)

分类:android开发网摘 136人阅读 评论(0) 收藏 举报

注:本文转自http://blog.163.com/shaocpa@126/blog/static/55357757201241102525166/

1.修改系统默认的Dialog样式(风格、主题)
2.自定义Dialog布局文件
3.可以自己封装一个类,继承自Dialog或者直接使用Dialog类来实现,为了方便以后重复使用,建议自己封装一个Dialog类
第一步:   我们知道Android定义个控件或View的样式都是通过定义其style来实现的,查看Android框架中的主题文件,在源码中的路径:/frameworks/base/core/res/res/values/themes.xml,我们可以看到,Android为Dialog定义了一个样式,
[html] view plain copy
  1. <stylename="Theme.Dialog">
  2. <itemname="android:windowFrame">@null</item>
  3. <itemname="android:windowTitleStyle">@android:style/DialogWindowTitle</item>
  4. <itemname="android:windowBackground">@android:drawable/panel_background</item>
  5. <itemname="android:windowIsFloating">true</item>
  6. <itemname="android:windowContentOverlay">@null</item>
  7. <itemname="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
  8. <itemname="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
  9. </style>

我们可以看到,在Themes.xml中定义的Dialog的样式,其中,定义了window的标题样式,window的背景图,是否悬浮等等。
  那么,我们要创建具有自定义样式的Dialog就可以创建一个styles.xml,在其中定义我们自己的Dialog样式,让其继承自Theme.Dialog样式,并修改其中的某些属性即可。
  定义我们自己的Dialog样式:
  a.创建一个styles.xml文件,放在res/values 文件夹下(当然了,这就不用说了。。。啰嗦一下)
  b.在styles.xml中定义Dialog样式,代码如下:
[html] view plain copy
  1. <stylename="Theme_dialog"parent="@android:style/Theme.Dialog">
  2. <itemname="android:windowBackground">@android:color/transparent</item>
  3. <itemname="android:windowNoTitle">true</item>
  4. </style>

  上面代码中,定义了一个样式Theme_dialog,继承自@android:style/Theme.Dialog,然后定义了Dialog所在Window的背景图,此处使用的是透明颜色#00000000,然后定义了Dialog所在的Window隐藏标题(系统定义Dialog样式是带有标题的,在此设置此属性为true可隐藏标题),自定义Dialog样式到这就完成了。
第二步:
  定义Dialog的布局:   创建一个布局文件layout_dialog.xml,代码如下: [html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. android:background="@drawable/pp_bg_dialog"
  8. android:gravity="center">
  9. <ProgressBarandroid:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. style="@style/progressbar_normal"/>
  12. <TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"
  13. style="@style/text_white_small"android:layout_marginTop="5dp"
  14. android:text="正在删除..."android:id="@+id/message"/>
  15. </LinearLayout>

这里使用了一个垂直方向的线性布局,并且设置所有子元素居中,子元素为已个进度条ProgressBar和一个TextView。
  此处,ProgressBar采用自定义样式,使用系统默认的ProgressBar可达到同样的效果(大同小异)。LinearLayout的背景
android:background="@drawable/pp_bg_dialog"(即上面效果图中居中显示的黑色透明背景框)是一个自定义的图片资源Shape:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <shape
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:shape="rectangle">
  5. <cornersandroid:radius="10dp"/>
  6. <solidandroid:color="#55000000"/>
  7. </shape>

代码中定义了一个矩形,并设置了圆角和颜色。到此,Dialog的布局就完成了。 第三步:   自定义CustomDialog类,继承自Dialog,代码如下: [html] view plain copy
  1. publicclassCustomDialogextendsDialog{2
  2. privatestaticintdefault_width=160;//默认宽度
  3. privatestaticintdefault_height=120;//默认高度
  4. publicCustomDialog(Contextcontext,intlayout,intstyle){
  5. this(context,default_width,default_height,layout,style);
  6. }
  7. publicCustomDialog(Contextcontext,intwidth,intheight,intlayout,intstyle){
  8. super(context,style);12
  9. //setcontent
  10. setContentView(layout);
  11. //setwindowparams
  12. Windowwindow=getWindow();
  13. WindowManager.LayoutParamsparams=window.getAttributes();
  14. //setwidth,heightbydensityandgravity
  15. floatdensity=getDensity(context);
  16. params.width=(int)(width*density);
  17. params.height=(int)(height*density);
  18. params.gravity=Gravity.CENTER;
  19. window.setAttributes(params);
  20. }
  21. privatefloatgetDensity(Contextcontext){
  22. Resourcesresources=context.getResources();
  23. DisplayMetricsdm=resources.getDisplayMetrics();
  24. returndm.density;
  25. }
  26. }

在构造方法中设置了Dialog的contentView,并且设置了Window的宽度、高度和居中显示。
CustomDialog使用方法如下:
[html] view plain copy
  1. publicclassCustomDialogDemoActivityextendsActivity{
  2. @Override
  3. protectedvoidonCreate(BundlesavedInstanceState){
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.test);
  6. CustomDialogdialog1=newCustomDialog(this,R.layout.common_dialog,R.style.Theme_dialog);//Dialog使用默认大小(160)
  7. CustomDialogdialog2=newCustomDialog(this,180,180,R.layout.common_dialog,R.style.Theme_dialog);
  8. dialog2.show();//显示Dialog
  9.     //如果要修改Dialog中的某个View,比如把"正在删除..."改为"加载中..."
  10. TextViewmMessage=(TextView)dialog2.findViewById(R.id.message);
  11. mMessage.setText("加载中...");
  12. }
  13. }

大体过程就是这样,根据以上大家可以自由发挥吧,希望都设计出自己满意的dialog。

更多相关文章

  1. Android UI 开发入门—线性布局
  2. Android自定义属性 及 TypedArray的使用方法
  3. Android Studio使用XML样式在JAVA代码中的使用(使用java代码调节x
  4. Android自定义TabLayout后ViewPager与TabLayout互相控制切换
  5. Android 自定义字体方案
  6. Android Studio自定义模板:简单自定义DeviceAdminReceiver模板
  7. Android FragmentTransactionExtended:使Fragment以多种样式动画
  8. Android 之布局(一)

随机推荐

  1. Android框架学习笔记02AndroidAsycHttp框
  2. Android异步处理系列文章
  3. Android(安卓)密钥保护和 C/S 网络传输安
  4. Android图形选择 - Selector
  5. Android 摄像头
  6. Android动态效果Animation
  7. Android TV Audio基本框架及启动流程分析
  8. android studio for android learning (九
  9. [android]android自动化测试八之让你的AV
  10. Android监听事件四种方法