将MaterialAlertDialogBu​​ilder与材质主题一起使用

时间:2019-07-18 18:02:33

标签: android

我已将android应用程序设置为使用材质主题

<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.

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

2 个答案:

答案 0 :(得分:0)

以下为我工作

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 API的多个style.xml文件。

答案 1 :(得分:0)

只需这样做

 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>