首页 > 系统 > Android > 正文

Material风格的Dialog(android.support.v7.app.AlertDialog)

2019-11-07 23:40:02
字体:
来源:转载
供稿:网友

谷歌发布了Material Design设计之后,很多 Material 风格的控件也随之加入到了V7兼容包中。Android Support Library v22.1 中开始提供了Material风格的Dialog控件 。这为开发者提供了很好的支持,省去了使用开源库或自己设计的烦恼。下面我们来看看如何使用 Material风格的 Dialog。

兼容的 AlertDialog

拥有Material风格的Dialog控件在下列类:

android.support.v7.app.AlertDialog

所以想要使用此风格的对话框,需要在Module的build.gradle中导入

dependencies { compile 'com.android.support:appcompat-v7:23.1.1'}

这个V7包中的AlertDialog在Android2.1以上可以提供兼容性的 Material 风格 Dialog 。也就是说,使用这个包中的 AlertDialog 的话,从2.1到7.0都是Material风格的Dialog 。当使用这个包中的AlertDialog 时:

(左边为4.4,右边为5.1) (左边为4.4,右边为5.1)

非兼容的 AlertDialog


如果直接使用

android.app.AlertDialog

使用这个包中的AlertDialog的话,在 Android 5.0以下就是原始风格, 5.0 以上为 Material 风格。

我们来看看对比图: (左边为4.4,右边为5.1)

实例


在选择导入包的时候决定了这个是否兼容低版本,因为 Android 碎片化的原因,推荐使用V7包中的AlertDialog达到高低版本统一样式的效果。 这里用一个简单的Demo样式创建一个Dialog并显示的过程。

/** * 这是兼容的AlertDialog */PRivate void showDialog() { /* * 这里使用了 android.support.v7.app.AlertDialog.Builder * 可以直接在头部写 import android.support.v7.app.AlertDialog * 那么下面就可以写成 AlertDialog.Builder */ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Material Design Dialog"); builder.setMessage("这是 android.support.v7.app.AlertDialog 中的样式"); builder.setNegativeButton("取消", null); builder.setPositiveButton("确定", null); builder.show();}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表