Android的Material主题对话框:Material Dialogs

jopen 9年前

Material Dialogs这个库可以让您在所有版本的Android中使用统一的Material主题对话框。

Basic Dialog

Here's a basic example that mimics the dialog you see on Google's Material design guidelines (here: http://www.google.com/design/spec/components/dialogs.html#dialogs-usage). Note that you can always substitute literal strings and string resources for methods that take strings, the same goes for color resources (e.g. titleColor and titleColorRes).

new MaterialDialog.Builder(this)          .title("Use Google's Location Services?")          .content("Let Google help apps determine location. This means sending anonymous location data to Google, even when no apps are running.")          .theme(Theme.LIGHT)  // the default is light, so you don't need this line          .positiveText("Agree")  // the default is 'OK'          .negativeText("Disagree")  // leaving this line out will remove the negative button          .build()          .show();

On Lollipop (API 21), the Material dialog will automatically match the positiveColor (which is used on the positive action button) to the colorAccent attribute of your styles.xml theme.


Stacked Action Buttons

If you have multiple action buttons that together are too wide to fit on one line, the dialog will stack the buttons to be vertically orientated.

new MaterialDialog.Builder(this)          .title("Use Google's Location Services?")          .content("Let Google help apps determine location. This means sending anonymous location data to Google, even when no apps are running.")          .positiveText("Turn on speed boost right now!")          .negativeText("No thanks")          .build()          .show();

On a tablet, this will be no different that the basic example. On a smaller phone, they will stack.


Neutral Action Button

You can specify neutral text in addition to the positive and negative text. It will show the neutral action on the far left.

new MaterialDialog.Builder(this)          .title("Use Google's Location Services?")          .content("Let Google help apps determine location. This means sending anonymous location data to Google, even when no apps are running.")          .positiveText("Agree")          .negativeText("Disagree")          .neutralText("More info")          .build()          .show();

Callbacks

To know when the user selects an action button, you set a callback. There's three variations of the callback for the action buttons:

new MaterialDialog.Builder(this)          .callback(new MaterialDialog.SimpleCallback() {              @Override              public void onPositive(MaterialDialog dialog) {              }          });    new MaterialDialog.Builder(this)          .callback(new MaterialDialog.Callback() {              @Override              public void onPositive(MaterialDialog dialog) {              }                @Override              public void onNegative(MaterialDialog dialog) {              }          });    new MaterialDialog.Builder(this)          .callback(new MaterialDialog.FullCallback() {              @Override              public void onPositive(MaterialDialog dialog) {              }                @Override              public void onNegative(MaterialDialog dialog) {              }                @Override              public void onNeutral(MaterialDialog dialog) {              }          });

You can choose which one to use based on which actions you make visible, and which actions need to trigger an event. If you pass text to an action, it will become visible (not including the positive action which is always visible and will default to 'OK' unless you make the dialog a list dialog). You don't need a callback to make actions visible. But the dialog will not dismiss when an action is pressed if no callback is set for it.


项目主页:http://www.open-open.com/lib/view/home/1415322976105