Android的Material主題對話框:Material Dialogs

jopen 10年前發布 | 70K 次閱讀 Android開發 移動開發 Material Dialogs

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.baiduhome.net/lib/view/home/1415322976105

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!