【发布时间】:2025-12-14 05:20:02
【问题描述】:
几个小时以来一直在敲我的头,试图弄清楚这有什么问题。我在未显示的 ConstraintLayout 中有一个 ListView。我刚刚习惯使用 ConstraintLayout,所以你会原谅我的任何愚蠢错误。这些是我的布局文件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ListView
android:id="@+id/noteList"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
上面文件中的 ListView 根本不显示,但是当我用 RecyclerView 替换 ListView 时,会显示列表。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/noteList"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
我在 ListView 上做错了什么,导致它无法正常工作,但 RecyclerView 工作正常?
【问题讨论】:
-
为什么要将 layout_width 和 layout_height 设置为
0dp?那会是你的问题吗?我会根据你的需要选择match_parent或 _wrap_content` -
你对 ListView 和 RecyclerView 的行使用相同的布局吗?请同时添加行的布局。顺便说一句,两个 ViewGroup 的工作方式不同:例如,ListView 行中的高度
match_parent将在引擎盖下更改为wrap_content,而在 RecyclerVIew 中,它确实会导致项目填满整个屏幕 -
@sebasira 尝试将 layout_width 和 layout_height 调整为 match_parent 和 wrap_content 但仍然没有结果。
-
@BömachtBlau 是的,我愿意。在我运行它之后,它仍然不会显示在 Android Studio 预览中的设计视图和应用程序中。
-
@pirupius :尝试将 listview 直接放在 activity_main.xml 中,然后检查是否可以查看?不要使用 include 来测试目的
标签: android xml listview android-constraintlayout