# 材料对话框(pre2.0 版本,已弃用) [ ![核心](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/core?label=core&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/core) [ ![公地](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/commons?label=commons&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/commons) [![Build Status](https://travis-ci.org/afollestad/material-dialogs.svg)](https://travis-ci.org/afollestad/material-dialogs) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/0a4acc30a9ce440087f7688735359bb8)](https://www.codacy.com/app/drummeraidan_50/material-dialogs?utm_source=github.com&utm_medium=referral&utm_content=afollestad/material-dialogs&utm_campaign=Badge_Grade) [![GitHub license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/afollestad/material-dialogs/blob/master/LICENSE.txt) ![Screenshots](https://raw.githubusercontent.com/afollestad/material-dialogs/master/art/readmeshowcase.png) # 目录(核心) 1. [样例项目](https://github.com/afollestad/material-dialogs#sample-project) 2. [Gradle 依赖](https://github.com/afollestad/material-dialogs#gradle-dependency)1。[库](https://github.com/afollestad/material-dialogs#repository)2。[Core](https://github.com/afollestad/material-dialogs#core)3。[Commons](https://github.com/afollestad/material-dialogs#commons) 3. [有什么新鲜事吗?](https://github.com/afollestad/material-dialogs#whats-new) 4. [基本对话框](https://github.com/afollestad/material-dialogs#basic-dialog) 5. [取消对话框](https://github.com/afollestad/material-dialogs#dismissing-dialogs) 6. [显示图标](https://github.com/afollestad/material-dialogs#displaying-an-icon) 7. [堆叠动作按钮](https://github.com/afollestad/material-dialogs#stacked-action-buttons)1。[堆叠行为](https://github.com/afollestad/material-dialogs#stacking-behavior) 8. [中性动作按钮](https://github.com/afollestad/material-dialogs#neutral-action-button) 9. [回调](https://github.com/afollestad/material-dialogs#callbacks) 10. [复选框提示](https://github.com/afollestad/material-dialogs#checkbox-prompts) 11. [列出对话框](https://github.com/afollestad/material-dialogs#list-dialogs) 12. [单项选择列表对话框](https://github.com/afollestad/material-dialogs#single-choice-list-dialogs)1。[着色单选按钮](https://github.com/afollestad/material-dialogs#coloring-radio-buttons) 13. [多项选择列表对话框](https://github.com/afollestad/material-dialogs#multi-choice-list-dialogs)1。[着色复选框](https://github.com/afollestad/material-dialogs#coloring-check-boxes) 14. [将 IDS 分配给列表项视图](https://github.com/afollestad/material-dialogs#assigning-ids-to-list-item-views) 15. [自定义列表对话框](https://github.com/afollestad/material-dialogs#custom-list-dialogs) 16. [自定义视图](https://github.com/afollestad/material-dialogs#custom-views)1。[以后的访问](https://github.com/afollestad/material-dialogs#later-access) 17. [字体](https://github.com/afollestad/material-dialogs#typefaces) 18. [获取和设置动作按钮](https://github.com/afollestad/material-dialogs#getting-and-setting-action-buttons) 19. [主题化](https://github.com/afollestad/material-dialogs#theming)1。[基础知识](https://github.com/afollestad/material-dialogs#basics)2。[颜色](https://github.com/afollestad/material-dialogs#colors)3。[选择器](https://github.com/afollestad/material-dialogs#selectors)4。[重力](https://github.com/afollestad/material-dialogs#gravity)5。[材料调色板](https://github.com/afollestad/material-dialogs#material-palette) 20. [全球主题](https://github.com/afollestad/material-dialogs#global-theming) 21. [显示、取消和取消回调](https://github.com/afollestad/material-dialogs#show-cancel-and-dismiss-callbacks) 22. [输入对话框](https://github.com/afollestad/material-dialogs#input-dialogs)1。[为 EditText 着色](https://github.com/afollestad/material-dialogs#coloring-the-edittext)2。[限制输入长度](https://github.com/afollestad/material-dialogs#limiting-input-length)3。[自定义失效](https://github.com/afollestad/material-dialogs#custom-invalidation) 23. [进度对话框](https://github.com/afollestad/material-dialogs#progress-dialogs)1。[ProGuard ](https://github.com/afollestad/material-dialogs#proguard)2。[不确定进度对话框](https://github.com/afollestad/material-dialogs#indeterminate-progress-dialogs)3。[确定进度对话框](https://github.com/afollestad/material-dialogs#determinate-seek-bar-progress-dialogs)4。[在水平方向上创建一个不确定的对话框](https://github.com/afollestad/material-dialogs#make-an-indeterminate-dialog-horizontal)5。[为进度条着色](https://github.com/afollestad/material-dialogs#coloring-the-progress-bar)6。[自定义编号和进度格式](https://github.com/afollestad/material-dialogs#custom-number-and-progress-formats) 24. [Tint Helper ](https://github.com/afollestad/material-dialogs#tint-helper) 25. [Misc ](https://github.com/afollestad/material-dialogs#misc) # 目录(Commons) 1. [颜色选择对话框](https://github.com/afollestad/material-dialogs#color-chooser-dialogs)1。[寻找可见的对话框](https://github.com/afollestad/material-dialogs#finding-visible-dialogs)2。[用户颜色输入](https://github.com/afollestad/material-dialogs#user-color-input) 2. [文件选择器对话框](https://github.com/afollestad/material-dialogs#file-selector-dialogs) 3. [文件夹选择器对话框](https://github.com/afollestad/material-dialogs#folder-selector-dialogs) 4. [偏好对话框](https://github.com/afollestad/material-dialogs#preference-dialogs) 5. [简单列表对话框](https://github.com/afollestad/material-dialogs#simple-list-dialogs) ------ # Sample Project 你可以从这里库下载最新的示例 APK:https://github.com/afollestad/ material-dialogs /blob/master/sample/sample.apk 它也在 Google Play 上: 安装示例项目是了解新版本的一个好方法。尽管观看这篇文章库将允许 Github 在我发布新闻稿时给你发电子邮件。 --- # Gradle 依赖 ### Repository Gradle 依赖项可通过 [jCenter](https://bintray.com/drummer-aidan/maven/material-dialogs:core/view) 获得。JCenter 是 Android Studio 使用的默认 Maven库。 此库支持的最小 API 级别是 API14。 ### Core 核心模块包含了这个库的所有主要类,包括 `MaterialDialog`。你可以使用 Core 创建基本的、列表的、单 / 多选择的、进度的、输入的等对话框。 ```gradle dependencies { // ... other dependencies here implementation 'com.afollestad.material-dialogs:core:0.9.6.0' } ``` ### Commons Commons 模块包含对库的扩展,并不是每个人都可能需要。这包括 `ColorChooserDialog`,`FolderChooserDialog`,材料 `Preference` 类,以及 `MaterialSimpleListAdapter`/`materialsimplelistitem`。 ```gradle dependencies { // ... other dependencies here implementation 'com.afollestad.material-dialogs:commons:0.9.6.0' } ``` 新的扩展很可能会在以后添加到 Commons 中。 --- # What's New 请参阅项目的发布页面,获取带有更改日志的版本列表。 ### [View Releases](https://github.com/afollestad/material-dialogs/releases) 如果你看了这个库,每次我发布更新时,GitHub 都会给你发一封电子邮件。 --- # Basic Dialog 首先,注意 `MaterialDialog` 扩展了 `DialogBase`,这扩展了 `android.app.Dialog`。 下面是一个基本的示例,它模仿了你在 Google 的材料设计指南中看到的对话框(这里是:http://www.google.com/design/spec/components/dialogs.html#dialogs-usage)。请注意,对于接受字符串的方法,总是可以用文字字符串和字符串资源代替,对于颜色资源也是一样(例如 `titleColor` 和 `titleColorRes`)。 ```java new MaterialDialog.Builder(this) .title(R.string.title) .content(R.string.content) .positiveText(R.string.agree) .negativeText(R.string.disagree) .show(); ``` 你的活动需要继承 Appcompat 主题,以便正确地处理此库。材质对话框将自动将 `positiveColor`(用于正向操作按钮)与 styles.xml 主题的 `colorAccent` 属性匹配。 如果内容足够长,它将变得可以滚动,并且在操作按钮上方将显示一个分隔符。 --- # 取消对话框 我遇到过很多问题,问你如何拒绝对话。它的工作方式与 `AlertDialog` 相同,因为 `AlertDialog` 和 `MaterialDialog` 都是 `android.app.Dialog` 的实例(这就是 `dismiss()` 和 `show()` 的来源)。不能使用 `Builder` 取消对话框。你只能使用对话框本身来取消对话框。 有很多方法可以得到 `MaterialDialog` 的实例。两种主要的方法是通过 `show()` 和 `build()` 的方法 `MaterialDialog.Builder`。 通过 `show()`,它立即显示对话框并返回可见的对话框: ```java MaterialDialog dialog = new MaterialDialog.Builder(this) .title(R.string.title) .content(R.string.content) .positiveText(R.string.agree) .show(); ``` 通过 `build()`,它只构建对话框,但不显示它,直到你这么说: ```java MaterialDialog.Builder builder = new MaterialDialog.Builder(this) .title(R.string.title) .content(R.string.content) .positiveText(R.string.agree); MaterialDialog dialog = builder.build(); dialog.show(); ``` 一旦显示了对话框,你就可以取消它: ```java dialog.dismiss(); ``` 还有其他不同的地方给出了 `MaterialDialog` 实例,例如在下面的将来部分中讨论的一些回调中。 --- # 显示图标 MaterialDialog 支持显示一个图标,就像 Stock AlertDialog 一样;它将转到标题的左侧。 ```java new MaterialDialog.Builder(this) .title(R.string.title) .content(R.string.content) .positiveText(R.string.agree) .icon(R.drawable.icon) .show(); ``` 你可以使用 `limitIconToDefaultSize()`、`maxIconSize(int size)` 或 `maxIconSizeRes(int sizeRes)`Builder 方法来限制图标的最大大小。 --- # 堆叠动作按钮 如果你有多个动作按钮在一起太宽,不适合在一条线上,对话框将堆叠按钮,以垂直方向。 ```java new MaterialDialog.Builder(this) .title(R.string.title) .content(R.string.content) .positiveText(R.string.longer_positive) .negativeText(R.string.negative) .show(); ``` ### 堆叠行为 你可以从 `Builder` 设置堆叠行为: ```java new MaterialDialog.Builder(this) ... .stackingBehavior(StackingBehavior.ADAPTIVE) // the default value .show(); ``` --- # 中性动作按钮 除了肯定和否定的文本外,你还可以指定中性文本。它将显示最左翼的中立行动。 ```java new MaterialDialog.Builder(this) .title(R.string.title) .content(R.string.content) .positiveText(R.string.agree) .negativeText(R.string.disagree) .neutralText(R.string.more_info) .show(); ``` --- # Callbacks 从 0.8.2.0 版本开始,`callback()`Builder 方法已被弃用,取而代之的是下面讨论的各个回调方法。更早的版本仍然需要使用 `ButtonCallback`。 要知道用户何时选择动作按钮,你需要设置回调: ```java new MaterialDialog.Builder(this) .onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(MaterialDialog dialog, DialogAction which) { // TODO } }) .onNeutral(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(MaterialDialog dialog, DialogAction which) { // TODO } }) .onNegative(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(MaterialDialog dialog, DialogAction which) { // TODO } }) .onAny(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(MaterialDialog dialog, DialogAction which) { // TODO } }); ``` 如果你正在监听这三个动作按钮,你可以只使用 `onAny()`。`which` 参数将告诉你按下了哪个按钮。 如果 `autoDismiss` 已关闭,那么你必须手动取消这些回调中的对话框。默认情况下,自动删除是打开的。 --- # 复选框提示 复选框提示允许你显示一个 UI,类似于 Android 在 API23+ 上请求权限时使用的 UI。 注意:你也可以在列表对话框和输入对话框中使用复选框提示。 ```java new MaterialDialog.Builder(this) .iconRes(R.drawable.ic_launcher) .limitIconToDefaultSize() .title(R.string.example_title) .positiveText(R.string.allow) .negativeText(R.string.deny) .onAny(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(MaterialDialog dialog, DialogAction which) { showToast("Prompt checked? " + dialog.isPromptCheckBoxChecked()); } }) .checkBoxPromptRes(R.string.dont_ask_again, false, null) .show(); ``` --- # List Dialogs 创建一个列表对话框只需要传入一个字符串数组。回调(“itemscallback”)也很简单。 ```java new MaterialDialog.Builder(this) .title(R.string.title) .items(R.array.items) .itemsCallback(new MaterialDialog.ListCallback() { @Override public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) { } }) .show(); ``` 如果 `autoDismiss` 已关闭,则必须手动取消回调中的对话框。默认情况下,自动删除是打开的。你可以将 `positiveText()` 或其他操作按钮传递给构建器,以强制它显示列表下方的操作按钮,但是这仅在某些特定情况下有用。 --- # 单项选择列表对话框 单项选择列表对话框与常规列表对话框几乎相同。唯一的区别是,你使用 `itemsCallbackSingleChoice` 设置回调,而不是 `itemsCallback`。这表示对话框显示列表项旁边的单选按钮。 ```java new MaterialDialog.Builder(this) .title(R.string.title) .items(R.array.items) .itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() { @Override public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) { /** * If you use alwaysCallSingleChoiceCallback(), which is discussed below, * returning false here won't allow the newly selected radio button to actually be selected. **/ return true; } }) .positiveText(R.string.choose) .show(); ``` 如果你想预选一个项,请在 `itemsCallbackSingleChoice()` 中传递一个索引 0 或更大的值来代替-1。稍后,如果不使用自定义适配器,则可以在 `MaterialDialog` 实例上使用 `setSelectedIndex(int)` 更新选定的索引。 如果不使用 `positiveText()` 设置正向操作按钮,则当用户按下正向操作按钮时,对话框将自动调用单项选择回调。对话框也会自动删除,除非自动删除被关闭。 如果调用 `alwaysCallSingleChoiceCallback()`,则每当用户选择 / 取消某个项时,都会调用 single choice 回调。 ## 着色单选按钮 与动作按钮和材质对话框中的许多其他元素一样,你可以自定义对话框中单选按钮的颜色。`Builder` 类包含一个 `widgetColor()`、`widgetColorRes()`、`widgetColorAttr()` 和 `choiceWidgetColor()` 方法。它们的名称和参数注释使它们不言自明。 `widgetColor` 是影响其他 UI 元素的相同颜色。`choiceWidgetColor` 是单选和多选对话框特有的,它只影响单选按钮和复选框。你提供的是 `ColorStateList`,而不是用于生成 `ColorStateList` 的单一颜色。 请注意,默认情况下,单选按钮将使用活动主题中的 `colorAccent`(用于 AppCompat)或 `android:colorAccent`(用于材料主题)中的颜色进行着色。 还有一个全局主题化属性,如本文自述的全局主题化部分所示:`md_widget_color`。 --- # 多项选择列表对话框 多项选择列表对话框与常规列表对话框几乎相同。唯一的区别是,你使用 `itemsCallbackMultiChoice` 设置回调,而不是 `itemsCallback`。这表示对话框显示列表项旁边的复选框,回调可以返回多个选择。 ```java new MaterialDialog.Builder(this) .title(R.string.title) .items(R.array.items) .itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() { @Override public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) { /** * If you use alwaysCallMultiChoiceCallback(), which is discussed below, * returning false here won't allow the newly selected check box to actually be selected * (or the newly unselected check box to be unchecked). * See the limited multi choice dialog example in the sample project for details. **/ return true; } }) .positiveText(R.string.choose) .show(); ``` 如果要预选任何项,请在 `itemsCallbackMultiChoice()` 中传递一个索引(资源或文字)数组来代替 null。稍后,如果不使用自定义适配器,则可以在 `MaterialDialog` 实例上使用 `setSelectedIndices(Integer[])` 更新选定的索引。 如果你没有使用 `positiveText()` 设置一个积极的动作按钮,当用户按下积极的动作按钮时,对话框将自动调用多项选择回调。对话框也会自动删除,除非自动删除被关闭。 如果调用 `alwaysCallMultiChoiceCallback()`,则每当用户选择 / 取消选择一个项时,都会调用多选择回调。 ## 着色复选框 与动作按钮和材质对话框中的许多其他元素一样,你可以自定义对话框复选框的颜色。`Builder` 类包含一个 `widgetColor()`、`widgetColorRes()`、`widgetColorAttr()` 和 `choiceWidgetColor()` 方法。它们的名称和参数注释使它们变得不言自明。 `widgetColor` 是影响其他 UI 元素的相同颜色。`choiceWidgetColor` 是单选和多选对话框特有的,它只影响单选按钮和复选框。你提供的是 `ColorStateList`,而不是用于生成 `ColorStateList` 的单一颜色。 请注意,默认情况下,单选按钮将使用活动主题中的 `colorAccent`(用于 AppCompat)或 `android:colorAccent`(用于材料主题)中的颜色进行着色。 还有一个全局主题化属性,如本文自述的全局主题化部分所示:`md_widget_color`。 --- # 将 IDS 分配给列表项视图 如果需要通过 ID 而不是索引来保留列表项跟踪,则可以从整数数组中分配项目 ID: ```java new MaterialDialog.Builder(this) .title(R.string.socialNetworks) .items(R.array.socialNetworks) .itemsIds(R.array.itemIds) .itemsCallback(new MaterialDialog.ListCallback() { @Override public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) { Toast.makeText(Activity.this, which + ": " + text + ", ID = " + view.getId(), Toast.LENGTH_SHORT).show(); } }) .show(); ``` 你还可以传递一个字面上的整数数组来代替数组资源 ID。 --- # 自定义列表对话框 与 Android 的本机对话框一样,你也可以通过 `.adapter()` 传入自己的适配器,以定制你希望列表工作的确切方式。 ```java new MaterialDialog.Builder(this) .title(R.string.socialNetworks) // second parameter is an optional layout manager. Must be a LinearLayoutManager or GridLayoutManager. .adapter(new ButtonItemAdapter(this, R.array.socialNetworks), null) .show(); ``` 注意,随着新版本的发布,材质对话框不再支持 `ListView` 和 `ListAdapter`。现在是每个人都使用 `RecyclerView` 的时候了。你的自定义适配器将不得不自行处理项目单击事件;这个库的类和示例项目提供了一些很好的示例,说明如何正确地完成这些操作。 如果需要访问 `RecyclerView`,可以使用 `MaterialDialog` 实例: ```java MaterialDialog dialog = new MaterialDialog.Builder(this) ... .build(); RecyclerView list = dialog.getRecyclerView(); // Do something with it dialog.show(); ``` 请注意,你不需要使用自定义适配器来访问 `RecyclerView`,它可以用于单 / 多选择对话框、常规列表对话框等。 --- # Custom Views 自定义视图非常容易实现。 ```java boolean wrapInScrollView = true; new MaterialDialog.Builder(this) .title(R.string.title) .customView(R.layout.custom_view, wrapInScrollView) .positiveText(R.string.positive) .show(); ``` 如果 `wrapInScrollView` 为真,那么库将把你的自定义视图放入滚动视图中。这允许用户在必要时滚动你的自定义视图(小屏幕、长内容等)。然而,在某些情况下,你并不想要这种行为。这主要包括在自定义布局中使用滚动视图的情况,包括列表视图、recyclerviews、Webviews、Gridviews 等。示例项目包含对此参数同时使用 true 和 false 的示例。 当 `wrapInScrollView` 为真时,你的自定义视图将自动在其周围放置填充。否则,你将负责使用看起来不错的填充值来填充你的内容。 ## Later Access 如果在构建对话框后需要访问自定义视图中的一个视图,则可以使用 `getCustomView()` 的 `MaterialDialog`。如果你将布局资源传递给 `Builder`,这将特别有用,对话框将为你处理视图膨胀。 ```java MaterialDialog dialog = //... initialization via the builder ... View view = dialog.getCustomView(); ``` --- # Typefaces 如果你想使用自定义字体,可以在使用 `Builder` 时调用 `typeface(String, String)`。这将从项目的 `assets/fonts` 文件夹中的文件中提取字体。例如,如果在 `/src/main/assets/fonts` 中有 `Roboto.ttf` 和 `Roboto-Light.ttf`,则将调用 `typeface("Roboto.ttf", "Roboto-Light.ttf")`。此方法还将通过 `TypefaceHelper` 处理回收字体,你可以在自己的项目中使用该方法来避免重复分配。RAW`typeface(Typeface, Typeface)` 变体不会回收字体,每次调用都会重新分配该字体。 还有一个全局主题属性,可以自动将字体应用到应用程序中的每个材料对话框中。 --- # 获取和设置动作按钮 如果你希望在对话框构建和显示之后获得对其中一个对话框操作按钮的引用(例如,启用或禁用按钮): ```java MaterialDialog dialog = //... initialization via the builder ... View negative = dialog.getActionButton(DialogAction.NEGATIVE); View neutral = dialog.getActionButton(DialogAction.NEUTRAL); View positive = dialog.getActionButton(DialogAction.POSITIVE); ``` 如果你想要更新对话框操作按钮的标题(你也可以传递一个字符串资源 ID 来代替文字字符串): ```java MaterialDialog dialog = //... initialization via the builder ... dialog.setActionButton(DialogAction.NEGATIVE, "New Title"); ``` --- # Theming 在棒棒糖之前,如果不使用反射和自定义可拉伸,主题警报对话框基本上是不可能的。自 Kitkat 之后,Android 变得更加中性,但 AlertDialogs 继续使用 Holo Blue 作为标题和标题分隔符。棒棒糖的改进甚至更大,默认情况下,对话框中除了动作按钮外没有其他颜色。这使得主题化变得更加容易。 ## Basics 默认情况下,材质对话框将基于从创建对话框的上下文中检索到的 `?android:textColorPrimary` 属性应用轻主题或暗主题。如果颜色是浅色(例如更白),它会猜测活动使用的是暗主题,它会使用对话框的暗主题。反之亦然的灯光主题。你可以从 `Builder#theme()` 方法手动设置所使用的主题: ```java new MaterialDialog.Builder(this) .content("Hi") .theme(Theme.DARK) .show(); ``` 或者,你也可以使用全局主题化属性,这将在下面的一节中讨论。全局主题避免了为你显示的每个对话框不断调用主题设置器。 ## Colors 用这个库创建的对话框的几乎每个方面都可以着色: ```java new MaterialDialog.Builder(this) .titleColorRes(R.color.material_red_500) .contentColor(Color.WHITE) // notice no 'res' postfix for literal color .linkColorAttr(R.attr.my_link_color_attr) // notice attr is used instead of none or res for attribute resolving .dividerColorRes(R.color.material_pink_500) .backgroundColorRes(R.color.material_blue_grey_800) .positiveColorRes(R.color.material_red_500) .neutralColorRes(R.color.material_red_500) .negativeColorRes(R.color.material_red_500) .widgetColorRes(R.color.material_red_500) .buttonRippleColorRes(R.color.material_red_500) .show(); ``` 这些名字在很大程度上是不言自明的。在本教程的其他章节中讨论的 `widgetColor` 方法适用于进度条、复选框和单选按钮。还需要注意的是,这些方法中的每一种都有 3 种变体,用于直接设置颜色、使用颜色资源和使用颜色属性。 ## Selectors 选择器是当被按下或聚焦时改变状态的可读物。 ```java new MaterialDialog.Builder(this) .btnSelector(R.drawable.custom_btn_selector) .btnSelector(R.drawable.custom_btn_selector_primary, DialogAction.POSITIVE) .btnSelectorStacked(R.drawable.custom_btn_selector_stacked) .listSelector(R.drawable.custom_list_and_stackedbtn_selector) .show(); ``` 第一个 `btnSelector` 行设置了一个可绘制的选择器,用于所有动作按钮。第二个 `btnSelector` 行复写了仅用于正按钮的可画项。这就导致了正向按钮与中性和负向按钮具有不同的选择器。`btnSelectorStacked` 设置一个可拉伸的选择器,当按钮堆叠时使用,要么是因为没有足够的空间将它们全部放在一行上,要么是因为你在 `Builder` 上使用了 `forceStacked(true)`。`listSelector` 用于列表项,当你不使用自定义适配器时。 注: 与使用自定义动作按钮选择器有关的一个重要注意事项:确保你的选择器可拉引用像默认的可拉引用一样插入可拉引用-这对于正确的动作按钮填充很重要。 ## Gravity 你可能不太可能想在对话框中改变元素的重力,但这是有可能的。 ```java new MaterialDialog.Builder(this) .titleGravity(GravityEnum.CENTER) .contentGravity(GravityEnum.CENTER) .btnStackedGravity(GravityEnum.START) .itemsGravity(GravityEnum.END) .buttonsGravity(GravityEnum.END) .show(); ``` 这些都是不言自明的。`titleGravity` 设置对话标题的重力,`contentGravity` 设置对话内容的重力,`btnStackedGravity` 设置堆叠动作按钮的重力,`itemsGravity` 设置列表项的重力(当你不使用自定义适配器时)。 for,`buttonsGravity` 请参见:
START (Default) | Neutral | Negative | Positive |
CENTER | Negative | Neutral | Positive |
END | Positive | Negative | Neutral |