我知道当ListView为空时如何显示消息有很多答案,但我想发两条消息。
这是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
<ProgressBar
android:id="@+id/main_progressbar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"></ListView>
<ViewStub
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout="@layout/no_data_stub" />
</RelativeLayout>
正如您所看到的只有一种情况,它显示空信息,但我还需要另一种情况。
答案 0 :(得分:0)
我是这样做的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
<ProgressBar
android:id="@+id/main_progressbar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"></ListView>
<TextView
android:id="@+id/emptyTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:text="halo"/>
</RelativeLayout>
现在我可以致电emptyTextView
,所以:
ListView listView = (ListView) rootView.findViewById(android.R.id.list);
TextView emptyTextView = (TextView) rootView.findViewById(R.id.emptyTextView);
emptyTextView.setText("test");
listView.setEmptyView(emptyTextView);
现在我可以设置我想要的任何文字,例如检查互联网连接。