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?
As pointed out by Falmarri, that isn't a ListView
but a PreferenceActivity
.
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 a ScrollView
around a LinearLayout
. You can read more about it here: http://developer.android.com/reference/android/widget/ScrollView.html
Below is a very simple example:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Some text label here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="A button to click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Perhaps an input field here"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Some more text, and a check box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
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.
That's not a listview, that's a PreferenceActivity
. Take a look at the PrefereceActivity class
http://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 the getView()
method, just return the view you want to show. There are plenty of examples online.
精彩评论