Material Design没有样式化警报对话框

Mat*_*hew 160 android android-appcompat android-support-library material-design material-components-android

我已将appCompat材质设计添加到我的应用程序中,似乎警报对话框未使用我的primary,primaryDark或强调颜色.

这是我的基本风格:

<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>
Run Code Online (Sandbox Code Playgroud)

根据我的理解,对话框按钮文本也应该使用这些颜色.我的理解是错的还是我还需要做些什么?


解:

明确的答案让我走上正轨.

<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>
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 455

使用新的,com.google.android.material.dialog.MaterialAlertDialogBuilder您可以使用新的 android.support.v7.app.AlertDialog.

只需使用这样的代码:

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

并使用这样的风格:

 <!-- Base application theme. -->
 <style name="AppTheme" parent="Theme.MaterialComponents.Light">
    ...
    <item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog</item>
 </style>
Run Code Online (Sandbox Code Playgroud)

否则,您可以在当前主题中定义:

new MaterialAlertDialogBuilder(context, R.style.ThemeOverlay_MaterialComponents_MaterialAlertDialog)
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中:

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();
Run Code Online (Sandbox Code Playgroud)

这里是Kitkat上的AlertDialog: 在此输入图像描述

  • 这应该是接受的答案,因为它使用本机视图而不是第三方库. (63认同)
  • 我必须在"android:"前面加上它才能工作:`<item name ="android:alertDialogTheme"> @ style/AppCompatAlertDialogStyle </ item>` (9认同)
  • @TomášHubálek你可以做到.答案已更新. (3认同)
  • 为什么要强制AlertDialog.Builder使用精确主题而不是更新默认主题?我更愿意在styles.xml而不是Java代码中解决这个问题. (2认同)
  • 在棒棒糖前设备上,标题和按钮字体不是粗体. (2认同)

San*_*mar 9

初始化对话框构建器时,将第二个参数作为主题传递.它将自动显示API级别为21的材料设计.

AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
Run Code Online (Sandbox Code Playgroud)

要么,

AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
Run Code Online (Sandbox Code Playgroud)


nad*_*ima 5

AppCompat 不会对对话框执行此操作(至少目前还没有)

编辑: 现在确实如此。确保使用android.support.v7.app.AlertDialog