警报对话框内的首选项活动

发布于 2025-01-08 20:57:36 字数 460 浏览 3 评论 0原文

我可以在警报对话框中填充 PreferenceScreen 吗?

这就是我尝试过的:

final LayoutInflater factory = LayoutInflater.from(this);
final View myDialog = factory.inflate(R.xml.prefScr, null);
final Dialog dialog = new AlertDialog.Builder(this)
    .setTitle("This is Dialog")
    .setView(myDialog)
    .create();

这给了我例外:

android.view.InflateException: Binary XML file line #2: Error inflating class PreferenceScreen

Can I inflate PreferenceScreen inside an alert dialog?

This is what I've tried:

final LayoutInflater factory = LayoutInflater.from(this);
final View myDialog = factory.inflate(R.xml.prefScr, null);
final Dialog dialog = new AlertDialog.Builder(this)
    .setTitle("This is Dialog")
    .setView(myDialog)
    .create();

this gives me exception:

android.view.InflateException: Binary XML file line #2: Error inflating class PreferenceScreen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌伤浅笑 2025-01-15 20:57:36

我认为不可能像这样在 PreferenceActivity 之外使用 PreferenceScreen...但这里有一些替代方案:

1)制作 Activity 通过使用 Manifest 中的样式显示为对话框:

<activity
    android:name=".MyPrefsActivity"
    android:label=""
    android:theme="@android:style/Theme.Dialog">
</activity>

2) 按照当前的方式使用 AlertDialog,但使用您自己的自定义布局对其进行扩充,该布局具有与您相同的功能PreferenceScreen 确实如此。然后您将必须手动管理首选项的获取和设置。

3) 只需使用 PreferenceActivity 及其自己的 PreferenceDialog。这可能与您真正想要的不同,但使用 PreferenceActivity 将为您节省大量手动处理所有首选项的工作。

I don't think it is possible to use a PreferenceScreen outside of a PreferenceActivity like this...but here are a few alternatives:

1) Make the Activity appear as a dialog by using a style in your Manifest:

<activity
    android:name=".MyPrefsActivity"
    android:label=""
    android:theme="@android:style/Theme.Dialog">
</activity>

2) Use an AlertDialog as you currently are but inflate it with your own custom layout that has the same features as you PreferenceScreen does. Then you would have to manage the getting and setting of preferences manually.

3) Just use a PreferenceActivity with its own PreferenceDialog. This might not really be the same thing that you want, but using a PreferenceActivity will save you a lot of the work of manually handling all of your preferences.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文