在 Jetpack Compose 中添加 AlertDialog 时获取 "Can't find ColorStateList from drawable resource ID"
Getting "Can't find ColorStateList from drawable resource ID" when adding AlertDialog in Jetpack Compose
我在我的应用程序中添加 AlertDialog
时遇到此 Can't find ColorStateList from drawable resource ID #0x108028b
错误。
我也尝试关注 this answer,但没有用。如果 android:statusBarColor
是问题,那么这是我在 night\themes.xml
中使用的代码
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.AppName" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<!-- <item name="statusBarBackground">@color/statusBarDark</item>-->
<item name="android:statusBarColor">?android:attr/colorBackground</item>
<!-- Navigation bar color. -->
<item name="android:navigationBarColor">?android:attr/windowBackground</item>
<!-- Customize your theme here. -->
</style>
<style name="ThemeOverlay.AppName.FullscreenContainer" parent="">
<item name="fullscreenBackgroundColor">@color/light_blue_900</item>
<item name="fullscreenTextColor">@color/light_blue_A400</item>
</style>
</resources>
编写代码:
@Composable
fun AlertDialogSample() {
MaterialTheme {
Column {
val openDialog = remember { mutableStateOf(false) }
Button(onClick = {
openDialog.value = true
}) {
Text("Click me")
}
if (openDialog.value) {
AlertDialog(
onDismissRequest = {
openDialog.value = false
},
title = {
Text(text = "Dialog Title")
},
text = {
Text("Here is a text ")
},
confirmButton = {
Button(
onClick = {
openDialog.value = false
}) {
Text("This is the Confirm Button")
}
},
dismissButton = {
Button(
onClick = {
openDialog.value = false
}) {
Text("This is the dismiss Button")
}
}
)
}
}
}
}
我不确定到底是什么导致了这个问题,需要一些帮助来解决这个问题。谢谢。
解决了。
问题出在下一行,不知道为什么 ?android:attr/windowBackground
不起作用,但改为简单的 @color/yourColor
就解决了。
<item name="android:navigationBarColor">?android:attr/windowBackground</item>
我在我的应用程序中添加 AlertDialog
时遇到此 Can't find ColorStateList from drawable resource ID #0x108028b
错误。
我也尝试关注 this answer,但没有用。如果 android:statusBarColor
是问题,那么这是我在 night\themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.AppName" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<!-- <item name="statusBarBackground">@color/statusBarDark</item>-->
<item name="android:statusBarColor">?android:attr/colorBackground</item>
<!-- Navigation bar color. -->
<item name="android:navigationBarColor">?android:attr/windowBackground</item>
<!-- Customize your theme here. -->
</style>
<style name="ThemeOverlay.AppName.FullscreenContainer" parent="">
<item name="fullscreenBackgroundColor">@color/light_blue_900</item>
<item name="fullscreenTextColor">@color/light_blue_A400</item>
</style>
</resources>
编写代码:
@Composable
fun AlertDialogSample() {
MaterialTheme {
Column {
val openDialog = remember { mutableStateOf(false) }
Button(onClick = {
openDialog.value = true
}) {
Text("Click me")
}
if (openDialog.value) {
AlertDialog(
onDismissRequest = {
openDialog.value = false
},
title = {
Text(text = "Dialog Title")
},
text = {
Text("Here is a text ")
},
confirmButton = {
Button(
onClick = {
openDialog.value = false
}) {
Text("This is the Confirm Button")
}
},
dismissButton = {
Button(
onClick = {
openDialog.value = false
}) {
Text("This is the dismiss Button")
}
}
)
}
}
}
}
我不确定到底是什么导致了这个问题,需要一些帮助来解决这个问题。谢谢。
解决了。
问题出在下一行,不知道为什么 ?android:attr/windowBackground
不起作用,但改为简单的 @color/yourColor
就解决了。
<item name="android:navigationBarColor">?android:attr/windowBackground</item>