gpt4 book ai didi

android - 将 MaterialAlertDialogBu​​ilder 与 Material 主题一起使用

转载 作者:行者123 更新时间:2023-12-03 14:38:49 32 4
gpt4 key购买 nike

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

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

在我的 Activity/fragment 中,我试图调用 Material 警报对话框
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)

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

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.

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

最佳答案

只需使用 即可

 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>

关于android - 将 MaterialAlertDialogBu​​ilder 与 Material 主题一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57100308/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com