实现步骤:
1:xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.user.listviewtest3.views.MainActivity">
<ListView
android:id="@+id/listview3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
<TextView
android:id="@+id/custom_empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="暂无数据"
android:textSize="30sp"/>
</LinearLayout>
注意:这里的“空白View组件(custom_empty_view)”需要和“ListView组件”在同一个布局中,否则在java代码中会出现“空白View组件(custom_empty_view)“的对象为null;
2: Java代码
defaultEmptyView = (TextView) findViewById(R.id.custom_empty_view);
Log.e("defaultEmptyView","defaultEmptyView = "+defaultEmptyView);
listView3.setEmptyView(defaultEmptyView);
1:xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.user.listviewtest3.views.MainActivity">
<ListView
android:id="@+id/listview3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
<TextView
android:id="@+id/custom_empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="暂无数据"
android:textSize="30sp"/>
</LinearLayout>
注意:这里的“空白View组件(custom_empty_view)”需要和“ListView组件”在同一个布局中,否则在java代码中会出现“空白View组件(custom_empty_view)“的对象为null;
2: Java代码
defaultEmptyView = (TextView) findViewById(R.id.custom_empty_view);
Log.e("defaultEmptyView","defaultEmptyView = "+defaultEmptyView);
listView3.setEmptyView(defaultEmptyView);