使用 ListView 在 Android 中创建设置屏幕?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2027771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:15:48  来源:igfitidea点击:

Using a ListView to create a settings screen in Android?

androidlistview

提问by Tomas

I'm developing my first Android application, and I'd like to create a settings screen.

我正在开发我的第一个 Android 应用程序,我想创建一个设置屏幕。

I'd like the screen to have a similar look-and-feel as the native phone-settings screens and the native "create/edit alarm" screen. Thus with different kinds of (statically defined) items vertically stacked with a thin line between them.

我希望屏幕具有与本机电话设置屏幕和本机“创建/编辑警报”屏幕类似的外观。因此,不同种类的(静态定义的)项目垂直堆叠,它们之间有一条细线。

How do I define such screen?

我如何定义这样的屏幕?

I understand I can use the ListView, but this seems to be primarily meant for serving dynamic data using a ListAdapter, where each item is served in the same format. It seems to be possible to create different items (that is, some with checkbox, some with two text-lines, some with an icon) by creating my own ListAdapter and overriding getView but this seems like overkill. Should I be using a ListView for this purpose?

我知道我可以使用 ListView,但这似乎主要用于使用 ListAdapter 提供动态数据,其中每个项目都以相同的格式提供。似乎可以通过创建我自己的 ListAdapter 并覆盖 getView 来创建不同的项目(即,一些带有复选框,一些带有两个文本行,一些带有图标),但这似乎有点矫枉过正。我应该为此目的使用 ListView 吗?

回答by Dave Webb

There's no need to manually create and format a ListView- there are ways in the API to create Preference screens.

无需手动创建和格式化 a ListView- API 中有多种方法可以创建首选项屏幕。

The most obvious is PreferenceActivity.

最明显的是PreferenceActivity

You can define all your preferences in an XML file, a bit like a layout, and then load them with addPreferencesFromResource()or you can create a number of PreferenceScreenobjects in code and populate them with Preferenceobjects that way.

您可以在 XML 文件中定义所有首选项,有点像布局,然后加载它们,addPreferencesFromResource()或者您可以PreferenceScreen在代码中创建多个对象并以Preference这种方式用对象填充它们。

The best thing do would be to look at the API Demos application provided with the Android API. This contains lots of good examples of managing preferences. Here's how it creates preferences from codeand here's a sample preferences XML file. There are other examples showing more advanced things like preference dependencies and preference listeners.

最好的办法是查看随 Android API 提供的 API Demos 应用程序。这包含许多管理偏好的好例子。下面是它如何从代码创建首选项,这里是一个示例首选项 XML 文件。还有其他示例显示了更高级的内容,例如偏好依赖项和偏好侦听器。

回答by tbruyelle

Actually in the built-in alarm application, for editand createalarms, there are two activities, one for createand one for edit.

实际上在内置的闹钟应用程序中,对于编辑创建闹钟,有两种活动,一种用于创建,一种用于编辑

The Create Alarmactivity is the first one with the digital clock. The Edit Alarmactivity is started by clicking on a listed alarm from the Create Alarmactivity.

创建警报活动是第一个与数字时钟。单击“创建警报”活动中列出的警报可启动“编辑警报”活动。

Edit Alarmimplements PreferenceActivity, but Create Alarmis more complex (custom cursor adapter to list the alarms).

编辑警报实现PreferenceActivity,但创建警报更复杂(自定义光标适配器以列出警报)。

Have a look at the sources:

看看来源