如何在一个屏幕中使用两个ListView?

时间:2010-08-03 09:47:55

标签: android

我想在一个ListActivity中使用两个列表视图。我怎样才能做到这一点?请帮我在一个ListActivity中创建两个不同的列表视图。

由于 迪帕克

2 个答案:

答案 0 :(得分:4)

只需创建一个名为main.xml的XML文件,如:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:layout_height="fill_parent"
  >
    <ListView android:id="@+id/ListView01" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        />
    <ListView android:id="@+id/ListView02" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_below="@+id/ListView01"
        android:gravity="center_horizontal"
        />
</RelativeLayout>

在onCreate函数中通过:setContentView(R.layout.main);加载此xml文件。

ListView list1 = ((ListView) findViewById(R.id.ListView01));

允许您访问第一个列表,然后您可以将适配器应用于list1。通过交换1到2,您也可以访问第二个ListView。

答案 1 :(得分:1)

您还可以使用LinearLayout而不是RelativeLayout,具体取决于您希望列表视图显示的方式。以下XML有两个列表视图,一个在另一个之上。每个占据屏幕的一半。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#f00" >
    </ListView>

    <ListView
        android:id="@+id/listView2"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#0f0" >
    </ListView>

</LinearLayout>

无论布局如何,访问列表视图都是相同的。