gpt4 book ai didi

android - 我无法更改我的 AlertDialog 主题

转载 作者:行者123 更新时间:2023-11-29 20:59:21 25 4
gpt4 key购买 nike

我需要一些指南来解决 android 主题的问题。

我创建了一个自定义主题,以便将其应用于警报对话框。一切看起来都很好,但是当我尝试使用 ContextThemeWrapper 将它应用到实际对话框时,android.R.style.MyTheme 无法识别。

这是我在 MainActivity.java 中的代码:

// Build CheckBox Dialog
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder checkboxDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(),
android.R.style.MyTheme));

checkboxDialogBuilder
.setTitle("Pick the colors")
.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {

// indexSelected contains the index of item (of which checkbox checked)
@Override
public void onClick(DialogInterface dialog, int item) {
switch(item)
{
case 0:
// RGB choice
RGB = true;
break;

case 1:
// CMY choice
RGB = false;
break;
}
}
})

// Set the action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// user clicked on OK
setColorModeBackground();
}
})

.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// user clicked on Cancel
mDialog.dismiss();
}
});

AlertDialog customCheckboxDialog = checkboxDialogBuilder.create();

customCheckboxDialog.show();

return customCheckboxDialog;
}

在这一行中我得到一个错误,因为 android.R.style.Mytheme 显然不存在,即使我在 R.java 文件中看到它。

AlertDialog.Builder checkboxDialogBu​​ilder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.MyTheme));

R.java 文件:

public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.


Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.


Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.

API 11 theme customizations can go here.
*/

public static final int AppBaseTheme=0x7f060000;

/** Application theme. All customizations that are NOT specific to a
particular API-level can go here.
*/

public static final int AppTheme=0x7f060001;
public static final int MyTheme=0x7f060007;
/**
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.

*/
public static final int base=0x7f060002;
public static final int body=0x7f060004;
public static final int customDialogTheme=0x7f060005;
public static final int dialogTheme=0x7f060006;
public static final int title=0x7f060003;
}

这是 res/values/styles.xml 中的文件:

    <style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>

<!-- Application theme. -->
<style name="MyTheme" parent="AppBaseTheme">
<item name="android:alertDialogStyle">@style/customDialogTheme</item>
</style>

<style name="customDialogTheme" parent="@android:style/Theme.Dialog">
<item name="android:alertDialogStyle">@style/customDialogTheme</item>
<item name="android:fullDark">@drawable/dialog_body</item>
<item name="android:topDark">@drawable/dialog_title</item>
<item name="android:centerDark">@drawable/dialog_body</item>
<item name="android:bottomDark">@drawable/dialog_footer</item>
<item name="android:fullBright">@drawable/dialog_body</item>
<item name="android:centerBright">@drawable/dialog_body</item>
<item name="android:bottomBright">@drawable/dialog_footer</item>
<item name="android:bottomMedium">@drawable/dialog_footer</item>
<item name="android:centerMedium">@drawable/dialog_body</item>
</style>

提前致谢!

最佳答案

AlertDialog.Builder checkboxDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.MyTheme));

您访问的资源是您自己的,不是android,请尝试上面的解决方案。

关于android - 我无法更改我的 AlertDialog 主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26635997/

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