Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ConstraintLayout中的ListView未显示_Android_Xml_Listview_Android Constraintlayout - Fatal编程技术网

Android ConstraintLayout中的ListView未显示

Android ConstraintLayout中的ListView未显示,android,xml,listview,android-constraintlayout,Android,Xml,Listview,Android Constraintlayout,几个小时来我一直在想我到底做错了什么。我在ConstraintLayout中有一个未显示的ListView。我刚刚习惯使用ConstraintLayout,所以请原谅我犯的任何愚蠢的错误。这些是我的布局文件 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://s

几个小时来我一直在想我到底做错了什么。我在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做了什么错事,使它无法工作,而RecycleView工作正常?

如果您想在设计模式下预览ListView,您必须使用
工具
命名空间。 向XML文件中添加类似的内容
tools:listitem=“@android:layout/simple\u list\u item\u 1”

然后将
tools
名称空间添加到XML文件的顶部,如下所示
xmlns:tools=”http://schemas.android.com/tools“

因此,您的文件应该如下所示



这样,您就可以预览列表视图。

为什么要将布局宽度和布局高度设置为
0dp
?这可能是你的问题吗?我将根据您的需要使用
match_parent
或_wrap_content`是否分别对ListView和RecyclerView的行使用相同的布局?请同时添加行的布局。顺便说一句,这两个视图组的工作方式不同:例如,ListView行中的高度
match\u parent
将更改为
wrap\u content
,而在RecyclerVIew中,它将真正导致项目填满整个视图screen@sebasira尝试调整布局宽度和布局高度以匹配父级和换行内容,但还是没有结果。@BömachtBlau是的,我有。在我运行它之后,它仍然不会同时显示在Android Studio预览版的“设计”视图和应用程序中。@pirupius:尝试将listview直接放在activity_main.xml中,只需检查是否可以查看?不要将include用于测试purpose