首页 > 解决方案 > 约束布局下的ListView在预览中消失

问题描述

输出png

所以下面是我的活动 xml:我已经从支持库迁移到 androidx。

有没有办法做到这一点?

<?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">
        <AutoCompleteTextView
            android:id="@+id/partyname"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@drawable/rounded_edittext"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="5dp"
            android:layout_marginStart="5dp"
            android:hint="@string/firm_name"
            android:padding="5dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    <LinearLayout
        android:id="@+id/billform"
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginLeft="5dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:background="@drawable/rounded_buttons"
        app:layout_constraintTop_toBottomOf="@id/partyname">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <AutoCompleteTextView
                android:id="@+id/identifier"
                android:layout_height="50dp"
                android:layout_width="0dp"
                android:padding="5dp"
                android:layout_marginEnd="8dp"
                android:textSize="16sp"
                android:textAllCaps="true"
                android:hint="@string/code_article"
                android:background="@drawable/rounded_edittext"
                android:layout_marginRight="8dp"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/add"
                android:layout_toStartOf="@+id/add" />
            <Button
                android:id="@+id/add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/rounded_buttons"
                android:text="@string/add"
                android:textAllCaps="true"
                android:textSize="18sp"
                android:layout_centerVertical="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true" />
        </RelativeLayout>
    </LinearLayout>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/billform"
        app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

天知道为什么,listView 在预览中不可用。它在根是线性布局时起作用。我尝试在约束布局中创建线性布局,但没有成功

标签: androidlistviewandroid-listview

解决方案


android:layout_height="0dp"

这可能是您的问题,或者如果您正在谈论看不到项目,那么它可能是 android studio 中的一个错误,因为我尝试添加另一个 ConstraintLayout 却没有任何运气。

这个答案可能会有所帮助

我想将我的答案添加为评论,但 StackOverflow 不会让我这样做。


推荐阅读