当我设置list.setEmptyView(empty)
时,列表背景颜色为白色且我无法找到带有绿色的文本视图,但如果我对此行进行评论,则列表背景颜色将为是青色。
addedItems数组:
String[] addedItems = {};
这是loadList
函数:
private void loadList() {
ListView list = findViewById(R.id.list);
if(addedItems != null) {
itemsAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, addedItems);
list.setAdapter(itemsAdapter);
TextView empty = new TextView(this);
empty.setText("The list is empty");
empty.setBackgroundColor(Color.GREEN);
list.setEmptyView(empty);
list.setBackgroundColor(Color.CYAN);
}
}
这是我的main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ir.vahid.actionbarmenu.MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
</ListView>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:1)
手动(膨胀)将空视图添加到Listview的“父”视图中:
ListView my_list = (ListView) findViewById(R.id.my_list);
View emptyView = getLayoutInflater().inflate(R.layout.empty_view,null);
((ViewGroup)my_list.getParent()).addView(emptyView);
listView.setEmptyView(emptyView);
OR
您的TextView应放在ListView项目的正下方,其可见性设置为
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ir.vahid.actionbarmenu.MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
</ListView>
<TextView
android:id="@+id/empty_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:text="@string/emptyList" >
</TextView>
</android.support.constraint.ConstraintLayout>