热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用ListView在Android中创建一个设置屏幕?-UsingaListViewtocreateasettingsscreeninAndroid?

ImdevelopingmyfirstAndroidapplication,andIdliketocreateasettingsscreen.我正在开发我的第一个An

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吗?

2 个解决方案

#1


121  

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

无需手动创建和格式化ListView——在API中有创建首选项屏幕的方法。

The most obvious is 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 PreferenceScreen objects in code and populate them with Preference objects that way.

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

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 code and here's a sample preferences XML file. There are other examples showing more advanced things like preference dependencies and preference listeners.

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

#2


2  

Actually in the built-in alarm application, for edit and create alarms, there are two activities, one for create and one for edit.

实际上,在内置的警报应用程序中,对于编辑和创建警报,有两个活动,一个用于创建,一个用于编辑。

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

创建闹钟活动是第一个带有数字时钟的活动。编辑警报活动是通过单击创建警报活动中的列出的警报启动的。

Edit Alarm implements PreferenceActivity, but Create Alarm is more complex (custom cursor adapter to list the alarms).

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

Have a look at the sources:

看一下资料来源:

  • Create Alarm activity
  • 创建警报活动
  • Edit Alarm activity
  • 编辑报警活动

推荐阅读
author-avatar
铁血aaaaaaaaaaaaa_295
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有