ListView 空视图不会显示
ListView empty view will not show up
当我设置 list.setEmptyView(empty)
列表背景颜色为白色时,我 找不到绿色的文本视图 ,但是如果我在列表中注释这一行背景颜色将为青色。
已添加项目数组:
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>
将空视图手动(膨胀)添加到列表视图的 "parent" 视图:
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);
或
您的 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>
当我设置 list.setEmptyView(empty)
列表背景颜色为白色时,我 找不到绿色的文本视图 ,但是如果我在列表中注释这一行背景颜色将为青色。
已添加项目数组:
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>
将空视图手动(膨胀)添加到列表视图的 "parent" 视图:
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);
或
您的 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>