有時候會發生沒有資料可以塞入 ListView ,此時需要一個 TextView 頂替:
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="no data"/>
接下來就是把 TextView 裝進 ListView:
ListView listView = (ListView) findViewById(R.id.listView); listView.setEmptyView(findViewById(R.id.textView)); listView.setAdapter(adapter);
另外關於 layout_weight 的用法可以參考: Android 对Layout_weight属性完全解析以及使用ListView来实现表格。
Android Studio 1.3
compileSdkVersion 22
buildToolsVersion "23.0.0 rc3"
沒有留言:
張貼留言