Material Design not styling alert dialogs

為{幸葍}努か 提交于 2019-11-26 12:15:36

问题


I\'ve added the appCompat material design to my app and it seems that the alert dialogs are not using my primary, primaryDark, or accent colors.

Here is my base style:

<style name=\"MaterialNavyTheme\" parent=\"@style/Theme.AppCompat.Light.DarkActionBar\">
    <item name=\"colorPrimary\">@color/apptheme_color</item>
    <item name=\"colorPrimaryDark\">@color/apptheme_color_dark</item>
    <item name=\"colorAccent\">@color/apptheme_color</item>
    <item name=\"android:textColorPrimary\">@color/action_bar_gray</item>
</style>

Based on my understanding the dialogs button text should also use these colors. Am I wrong on my understanding or is there something more I need to do?


Solution:

The marked answer got me on the right track.

<style name=\"MaterialNavyTheme\" parent=\"@style/Theme.AppCompat.Light.DarkActionBar\">
    <item name=\"colorPrimary\">@color/apptheme_color</item>
    <item name=\"colorPrimaryDark\">@color/apptheme_color_dark</item>
    <item name=\"colorAccent\">@color/apptheme_color</item>
    <item name=\"android:actionModeBackground\">@color/apptheme_color_dark</item>
    <item name=\"android:textColorPrimary\">@color/action_bar_gray</item>
    <item name=\"sdlDialogStyle\">@style/DialogStyleLight</item>
    <item name=\"android:seekBarStyle\">@style/SeekBarNavyTheme</item>
</style>

<style name=\"StyledDialog\" parent=\"Theme.AppCompat.Light.Dialog\">
    <item name=\"colorPrimary\">@color/apptheme_color</item>
    <item name=\"colorPrimaryDark\">@color/apptheme_color_dark</item>
    <item name=\"colorAccent\">@color/apptheme_color</item>
</style>

回答1:


UPDATED ON Aug 2019 WITH The Material components for android library:

With the new Material components for Android library you can use the new com.google.android.material.dialog.MaterialAlertDialogBuilder class, which extends from the existing androidx.appcompat.AlertDialog.Builder class and provides support for the latest Material Design specifications.

Just use something like this:

new MaterialAlertDialogBuilder(context)
            .setTitle("Dialog")
            .setMessage("Lorem ipsum dolor ....")
            .setPositiveButton("Ok", /* listener = */ null)
            .setNegativeButton("Cancel", /* listener = */ null)
            .show();

You can use in your theme something like:

 <!-- Base application theme. -->
 <style name="AppTheme" parent="Theme.MaterialComponents.Light">
    ...
    <item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog</item>
 </style>

You can also use the constructor:

new MaterialAlertDialogBuilder(context, R.style.ThemeOverlay_MaterialComponents_MaterialAlertDialog)

WITH SUPPORT LIBRARY:

With the new AppCompat v22.1 you can use the new android.support.v7.app.AlertDialog.

Just use a code like this:

import android.support.v7.app.AlertDialog

AlertDialog.Builder builder =
       new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
builder.setTitle("Dialog");
builder.setMessage("Lorem ipsum dolor ....");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();

And use a style like this:

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">#FFCC00</item>
        <item name="android:textColorPrimary">#FFFFFF</item>
        <item name="android:background">#5fa3d0</item>
    </style>

Otherwise you can define in your current theme:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- your style -->
    <item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>
</style>

and then in your code:

 import android.support.v7.app.AlertDialog

    AlertDialog.Builder builder =
           new AlertDialog.Builder(this);

Here the AlertDialog on Kitkat:




回答2:


when initializing dialog builder, pass second parameter as the theme. It will automatically show material design with API level 21.

AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);

or,

AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);



回答3:


AppCompat doesn't do that for dialogs (not yet at least)

EDIT: it does now. make sure to use android.support.v7.app.AlertDialog




回答4:


You can consider this project: https://github.com/fengdai/AlertDialogPro

It can provide you material theme alert dialogs almost the same as lollipop's. Compatible with Android 2.1.




回答5:


You could use

Material Design Library

Material Design Library made for pretty alert dialogs, buttons, and other things like snack bars. Currently it's heavily developed.

Guide, code, example - https://github.com/navasmdc/MaterialDesignLibrary

Guide how to add library to Android Studio 1.0 - How do I import material design library to Android Studio?

.

Happy coding ;)




回答6:


Try this library:

https://github.com/avast/android-styled-dialogs

It's based on DialogFragments instead of AlertDialogs (like the one from @afollestad). The main advantage: Dialogs don't dismiss after rotation and callbacks still work.




回答7:


For some reason the android:textColor only seems to update the title color. You can change the message text color by using a

SpannableString.AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.MyDialogTheme));

AlertDialog dialog = builder.create();
                Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");
                wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                dialog.setMessage(wordtoSpan);
                dialog.show();


来源:https://stackoverflow.com/questions/26455919/material-design-not-styling-alert-dialogs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!