我想创建一个类似于声音设置屏幕的listView屏幕(在内置设置应用程序中,请参见下图),即我希望一些行具有文本 +复选框,其他行以文本 +“ pop up”按钮,有些行应该只有文字等。

完成此操作的最佳方法是什么?

alt text

有帮助吗?

解决方案

那不是listview,那是 PreferenceActivity. 。看一下优先级课程

http://developer.android.com/reference/android/preference/preferenceactivity.html

如果您真的想在listView中对每一行的视图不同(这是非常有效的),则必须创建自己的类以扩展 BaseAdapter. 。在里面 getView() 方法,只需返回要显示的视图即可。在线有很多示例。

其他提示

正如法尔玛里指出的那样,那不是 ListView 但是 PreferenceActivity.

如果您不能与 PreferenceActivity, ,最简单的方法来列出不同项目的列表,如果它们超越屏幕,则将滚动滚动,是放置一个 ScrollView 周围 LinearLayout. 。你可以在这里读更多关于它的内容: http://developer.android.com/reference/android/widget/scrollview.html

以下是一个非常简单的例子:

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

现在,此列表不包含足够的项目滚动,但是如果您不断添加更多元素,则它将一旦变得大于屏幕就开始滚动。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top