Android。不显示 TableLayout。 ImageView问题

Android. TableLayout does not displayed. ImageView problem

我是 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>

我还有另一个几乎相同的选项。但是这个没有显示。此外 java class 几乎没有按钮的动作侦听器,而 TableLayuout 则没有。但是另一个 java class 和 table 布局没有任何代码,就像那样

public class admpanel extends lk{
    private TableLayout table;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admpanel);
    }
}

就是这样。

P.S。另一个 TableLayout 没有显示,因为我忘记为片段使用“id”,然后我修复了它。但现在我是idk

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

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

此外,如果此 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>