Android:如何创建每行包含不同内容的列表视图屏幕
我正在创建一个类似于声音设置屏幕的列表视图屏幕(在内置的设置应用程序中,请参见下图),即我希望某些行具有文本+复选框,其他行具有文本+“弹出”按钮,某些行应该只有文本等。
实现此目的的最佳方法是什么?
I'm looking to create a listview screen similar to the Sound Setting screen (in the built in Settings app, see image below), i.e I want some rows to have text + checkboxes, other rows to have text + "pop up" buttons, some rows should have text only etc.
What is the best way to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Falmarri 所指出的,这不是一个
ListView
而是一个PreferenceActivity
。如果您无法使用
PreferenceActivity
,创建不同项目的列表(当它们超出屏幕时将滚动)的最简单方法是放置一个ScrollView
围绕LinearLayout
。您可以在这里阅读更多相关信息: http://developer.android.com/reference /android/widget/ScrollView.html下面是一个非常简单的例子:
现在这个列表没有包含足够的项目来滚动,但是如果你继续向其中添加更多元素,它就会在到达后立即开始滚动比屏幕大。
As pointed out by Falmarri, that isn't a
ListView
but aPreferenceActivity
.If you can't work with the
PreferenceActivity
, the simplest way to make a list of different items that will scroll if they out-grow the screen, is to place aScrollView
around aLinearLayout
. You can read more about it here: http://developer.android.com/reference/android/widget/ScrollView.htmlBelow is a very simple example:
Now this list doesn't contain enough items to scroll, but if you keep adding more elements to it it will start to scroll as soon as it gets bigger than the screen.
这不是列表视图,而是
PreferenceActivity
。看一下 PrefereceActivity 类http://developer.android.com/reference/ android/preference/PreferenceActivity.html
如果您确实希望列表视图中的每一行都有不同的视图(这是非常有效的),则必须创建自己的扩展
BaseAdapter.在
getView()
方法中,只需返回您想要显示的视图即可。网上有很多例子。That's not a listview, that's a
PreferenceActivity
. Take a look at the PrefereceActivity classhttp://developer.android.com/reference/android/preference/PreferenceActivity.html
If you really want to have a different view for each row in a listview (which is very valid), you'll have to create your own class that extends
BaseAdapter
. In thegetView()
method, just return the view you want to show. There are plenty of examples online.