Android 安卓不显示TableLayout。图像视图问题

Android 安卓不显示TableLayout。图像视图问题,android,tablelayout,Android,Tablelayout,我是Android新手,在某个地方我不知道它是如何工作的。我有这个XML代码 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="htt

我是Android新手,在某个地方我不知道它是如何工作的。我有这个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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainWindow"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
        android:id="@+id/katalog"
        android:layout_width="388dp"
        android:layout_height="179dp"
        app:layout_constraintBottom_toTopOf="@+id/button5"
        app:layout_constraintStart_toStartOf="parent">

        <TableRow
            android:id="@+id/row"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView                                  // problem is here
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:srcCompat="@tools:sample/avatars" />

            <LinearLayout
                android:id="@+id/linear"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView11"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Name"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textView12"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Description"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textView13"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Stock"
                    android:textSize="18sp" />

                <Button
                    android:id="@+id/button9"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Button" />
            </LinearLayout>
        </TableRow>

    </TableLayout>

    <Button
        android:id="@+id/button3"
        android:layout_width="135dp"
        android:layout_height="51dp"
        android:text="Stuff"
        app:backgroundTint="#8BC34A"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button5"
        android:layout_width="135dp"
        android:layout_height="51dp"
        android:layout_marginEnd="1dp"
        android:layout_marginRight="1dp"
        android:text="Person"
        app:backgroundTint="#8BC34A"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button6"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/button3" />

    <Button
        android:id="@+id/button6"
        android:layout_width="135dp"
        android:layout_height="51dp"
        android:layout_marginStart="1dp"
        android:layout_marginLeft="1dp"
        android:text="card"
        app:backgroundTint="#07464E"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button5" />

</androidx.constraintlayout.widget.ConstraintLayout>
这就行了

另外一个表格布局没有显示,因为我忘了用“id”来表示工件,所以我修正了它。但现在我想


编辑:如果我删除ImageView,代码将开始工作。如何使用图像视图?指出的问题

您的代码存在的问题是,代码没有为imageView指定适当的android:src或app:srcCompat,这是将UmageView充气到某个高度所必需的,或者它认为该高度为0dp,导致imageView不显示。工具:src仅用于在XML设计选项卡中显示图像,以了解视图的空间大小或外观。指定的图像不能用于在应用程序中显示它。我已更新了您的代码以获得更好的设计,并为您的ImageView()添加了正确的图像。将图像作为12_徽标保存在可绘制文件夹中。检查此代码,如果您有任何问题,请发表评论

此外,如果此TableLayout的目的是使用它显示项目列表,请切换到RecyclerView以获得更好的性能和方便的数据处理

此外,您的按钮具有固定的高度和宽度,这并不理想,因为某些设备的按钮可能会脱离屏幕,因此我为按钮添加了更好的代码

<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <TableLayout
        android:id="@+id/katalog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1"
        app:layout_constraintBottom_toTopOf="@id/button3">

        <TableRow
            android:id="@+id/row"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="120dp"
                android:layout_height="match_parent"
                android:layout_column="0"
                android:src="@drawable/a12_logo" />

            <LinearLayout
                android:id="@+id/linear"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:orientation="vertical">

            <TextView
                android:id="@+id/textView11"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Name"
                android:textSize="18sp" />

                <TextView
                    android:id="@+id/textView12"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Description"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textView13"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Stock"
                    android:textSize="18sp" />

                <Button
                    android:id="@+id/button9"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Button" />
            </LinearLayout>
        </TableRow>
    </TableLayout>

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Stuff"
        app:backgroundTint="#8BC34A"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="1dp"
        android:text="Person"
        app:backgroundTint="#8BC34A"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button6"
        app:layout_constraintStart_toEndOf="@+id/button3" />

    <Button
        android:id="@+id/button6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="1dp"
        android:text="card"
        app:backgroundTint="#07464E"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button5" />
</androidx.constraintlayout.widget.ConstraintLayout>


Hi@Specator你能用
android:src=“@mipmap/ic_launcher”
替换行
tools:srcCompat=“@tools:sample/avatars”
并运行它吗?@Kartik不工作。但是如果我要删除头像(imageView),它就开始工作了。@Kartik如果我要删除头像,它就可以工作了。但仅适用于
android:src=“@mipmap/ic_launcher”
如果您也扩展了AppCompatActivity,这将起作用。这就是工作!谢谢lot@Spectator很高兴我能帮上忙。
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <TableLayout
        android:id="@+id/katalog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1"
        app:layout_constraintBottom_toTopOf="@id/button3">

        <TableRow
            android:id="@+id/row"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="120dp"
                android:layout_height="match_parent"
                android:layout_column="0"
                android:src="@drawable/a12_logo" />

            <LinearLayout
                android:id="@+id/linear"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:orientation="vertical">

            <TextView
                android:id="@+id/textView11"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Name"
                android:textSize="18sp" />

                <TextView
                    android:id="@+id/textView12"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Description"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textView13"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Stock"
                    android:textSize="18sp" />

                <Button
                    android:id="@+id/button9"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Button" />
            </LinearLayout>
        </TableRow>
    </TableLayout>

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Stuff"
        app:backgroundTint="#8BC34A"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="1dp"
        android:text="Person"
        app:backgroundTint="#8BC34A"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button6"
        app:layout_constraintStart_toEndOf="@+id/button3" />

    <Button
        android:id="@+id/button6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="1dp"
        android:text="card"
        app:backgroundTint="#07464E"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button5" />
</androidx.constraintlayout.widget.ConstraintLayout>