我已将 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>
Run Code Online (Sandbox Code Playgroud)
在我的活动/片段中,我试图调用材料警报对话框
new MaterialAlertDialogBuilder(mAppContext)
.setTitle("Title")
.setMessage("Message")
.setPositiveButton("Ok", null)
.show();
Run Code Online (Sandbox Code Playgroud)
在运行时,这会引发以下 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)
Run Code Online (Sandbox Code Playgroud)
这不是自相矛盾吗?材料组件需要 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.
Run Code Online (Sandbox Code Playgroud)
在我使用材质主题的情况下,如何让警报对话框工作?
下面对我有用
样式.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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
确保您没有用于 diff api 的多个 style.xml 文件。
| 归档时间: |
|
| 查看次数: |
5387 次 |
| 最近记录: |