在另一个视图中嵌入ListView

时间:2011-05-30 01:59:08

标签: android listview

如何将listview嵌入到我已创建的布局中?例如在my_layout.xml中,我有类似的内容:

<linear layout>
<textview>
<*my list view goes here*>
<linear layout>

我创建了一个List活动,如下所示:

public class HelloListView extends ListActivity {

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

      }
}

谢谢!

1 个答案:

答案 0 :(得分:4)

你的xml my_layout.xml:

<linear layout>
<textview>
      <ListView android:id="@+id/list" 
        android:layout_height="match_parent"                 
        android:layout_width="match_parent"
        >
       </ListView>
<linear layout>

和你的java文件:

public class HelloListView extends Activity { // not use ListActivity

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout); 
    ListView lv = (ListView) this.findViewById(R.id.list); 
    // make something for List adapter
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));


    lv.setTextFilterEnabled(true);

  }

}

希望能帮到你