【问题标题】:Using MaterialAlertDialogBuilder with a material theme将 MaterialAlertDialogBu​​ilder 与材质主题一起使用
【发布时间】:2019-11-27 17:32:59
【问题描述】:

我已将安卓应用设置为使用材质主题

<style name="AppTheme" parent="AppTheme.Base"></style>

<style name="AppTheme.Base" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="android:textColorSecondary">@android:color/white</item>
        <item name="android:textColorHint">@android:color/darker_gray</item>
        <item name="searchViewStyle">@style/SearchViewStyle</item>
</style>

在我的活动/片段中,我正在尝试调用材料警报对话框

new MaterialAlertDialogBuilder(mAppContext)
                            .setTitle("Title")
                            .setMessage("Message")
                            .setPositiveButton("Ok", null)
                            .show();

在运行时,这会引发以下 IllegalArgumentException 异常

java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).
        at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:240)
        at com.google.android.material.internal.ThemeEnforcement.checkAppCompatTheme(ThemeEnforcement.java:211)
        at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:146)
        at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:78)
        at com.google.android.material.dialog.MaterialDialogs.getDialogBackgroundInsets(MaterialDialogs.java:55)
        at com.google.android.material.dialog.MaterialAlertDialogBuilder.<init>(MaterialAlertDialogBuilder.java:116)
        at com.google.android.material.dialog.MaterialAlertDialogBuilder.<init>(MaterialAlertDialogBuilder.java:102)
        at com.syl.app.fragments.AlarmNotificationDetailFragment$2.onClick(UserFragment.java:265)
        at android.view.View.performClick(View.java:5207)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:941)
        at android.view.View$PerformClick.run(View.java:21177)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5441)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

这不是矛盾吗?材质组件需要 AppCompat 主题,而应用程序使用材质主题。

android 文档也这么说

MaterialAlertDialogBuilder requires that your application use a Material Components theme (e.g., Theme.MaterialComponents.Light). Using a Material Components theme with MaterialAlertDialogBuilder will result in an AlertDialog that matches your appplication’s color, typography, and shape theming.

在我使用材质主题的情况下,如何让警报对话框工作?

【问题讨论】:

标签: android


【解决方案1】:

下面对我有用

style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorSecondary">@android:color/white</item>
        <item name="android:textColorHint">@android:color/darker_gray</item>
        <item name="drawerArrowStyle">@android:style/DeviceDefault.ButtonBar</item>
        <item name="searchViewStyle">@android:style/Widget.Material.SearchView</item>
    </style>


</resources>

Androidmanifest.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

请确保您没有用于 diff apis 的多个 style.xml 文件。

【讨论】:

    【解决方案2】:

    只要做到这一点

     Context context = new ContextThemeWrapper(SelectImageActivity.this, R.style.AppTheme2);
    MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(context);
    

    您的主题(如果您使用 MaterialComponents)

      <style name="AppTheme2" parent="Theme.MaterialComponents.Light.DarkActionBar">
                <!-- Customize your theme here. -->
                <item name="colorPrimary">@color/colorPrimaryDark</item>
                <item name="colorPrimaryDark">@color/yellow</item>
                <item name="colorAccent">@color/yellow</item>
            </style>
    

    【讨论】:

      猜你喜欢
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      相关资源
      最近更新 更多