@android:id/empty(非ListActivity编码实现方式)
layout文件如下所示:
<ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@android:id/empty" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="No Data" />
当前Activity继承ListActivity:
当ListView所对应的adapter为空的时候,android系统会渲染出id为@android:id/empty的控件。而当adapter为非空的时候,会正常渲染出ListView的控件。
………………………………
当前Activity继承非ListActivity的时候,也可以有如上的效果。Layout文件不变,然后通过下面的java代码实现:
this.listView = (ListView) findViewById(R.id.list);this.listView.setEmptyView(findViewById(android.R.id.empty));
当然,empty控件的id不一定要采用系统默认的id。所有的id都是可以自定义的。如下所示:
<ListView android:id="@+id/listView" ... /><TextView android:id="@+id/emptyElement" ... />and in the linked Activity:
this.listView = (ListView) findViewById(R.id.listView);this.listView.setEmptyView(findViewById(R.id.emptyElement));Does also work with a GridView...