为什么我的设备没有显示 table?

Why is my device not showing the table?

我是 Android 的新手。
我正在尝试显示可滚动的 table.
Android Studio 上的 XML 可视化工具显示正常,但我的设备没有显示。
在我的设备中,我只能看到第一个 TextView(routine_heading).
我应该怎么做才能克服这个问题?

下面是我使用的XML代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/routine_heading"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/routine_header"
        android:textAppearance="?android:textAppearanceLarge"/>
    <ScrollView
        android:id="@+id/layout"
        android:layout_height="wrap_content"
        android:scrollbars="horizontal|vertical"
        android:layout_width="match_parent"
        android:layout_marginTop="8dip"
        android:scrollbarStyle="outsideOverlay"
        android:fillViewport="false">
        <HorizontalScrollView
            android:id="@+id/horizontalView"
            android:layout_height="wrap_content"
            android:scrollbars="horizontal|vertical"
            android:layout_width="wrap_content"
            android:layout_marginTop="5dip">
            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/tlGridTable"
                android:stretchColumns="*">
                <TableRow
                    android:layout_weight="1">
                    <TextView
                        android:background="@drawable/cell"
                        android:text=""
                        android:layout_height="match_parent"
                        android:layout_width="0dp"
                        android:layout_weight="1"/>
                    <TextView
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:background="@drawable/cell"
                        android:text="10-10:50"
                        android:padding="3dip"
                        android:layout_height="match_parent"
                        />
                    <TextView
                        android:text="10:50-11:40"
                        android:background="@drawable/cell"
                        android:padding="3dip"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        />
                    <TextView
                        android:background="@drawable/cell"
                        android:text="11:40-12:30"
                        android:padding="3dip"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="match_parent"
                        />
                    <TextView
                        android:background="@drawable/cell"
                        android:text="12:30-1:20"
                        android:padding="3dip"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="match_parent"
                        />
                    <TextView
                        android:background="@drawable/cell"
                        android:text="2:35-3:20"
                        android:padding="3dip"
                        android:layout_height="match_parent"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        />
                    <TextView
                        android:background="@drawable/cell"
                        android:text="3:20-4:00"
                        android:padding="3dip"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="match_parent"
                        />
                    <TextView
                        android:background="@drawable/cell"
                        android:text="4:00-4:50"
                        android:padding="3dip"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        />
                </TableRow>
             </TableLayout>
        </HorizontalScrollView>
    </ScrollView>
</LinearLayout>

如果你想要滚动table,你通常使用ListView + 一个Adapter

A​​dapter 的每个项目都应该创建 "table" 的 "row"。

使用 RecyclerView 可以帮助垂直和水平滚动。

像这样在滚动视图(进入子视图)下方添加 Table 布局

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
  <TextView
      android:id="@+id/routine_heading"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="@string/app_name"
      android:textAppearance="?android:textAppearanceLarge"
      />
  <ScrollView
      android:id="@+id/layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="8dip"
      android:fillViewport="false"
      android:scrollbarStyle="outsideOverlay"
      android:scrollbars="horizontal|vertical"
      >
    <TableLayout
        android:id="@+id/tlGridTable"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stretchColumns="*"
        >
      <HorizontalScrollView
          android:id="@+id/horizontalView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="5dip"
          android:scrollbars="horizontal|vertical"
          >

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >
          <TextView
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:background="@drawable/ic_launcher"
              android:text=""
              />

          <TextView
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:background="@drawable/ic_launcher"
              android:padding="3dip"
              android:text="10-10:50"
              />
          <TextView
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:background="@drawable/ic_launcher"
              android:padding="3dip"
              android:text="10:50-11:40"
              />
          <TextView
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:background="@drawable/ic_launcher"
              android:padding="3dip"
              android:text="11:40-12:30"
              />
          <TextView
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:background="@drawable/ic_launcher"
              android:padding="3dip"
              android:text="12:30-1:20"
              />
          <TextView
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:background="@drawable/ic_launcher"
              android:padding="3dip"
              android:text="2:35-3:20"
              />
          <TextView
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:background="@drawable/ic_launcher"
              android:padding="3dip"
              android:text="3:20-4:00"
              />
          <TextView
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:background="@drawable/ic_launcher"
              android:padding="3dip"
              android:text="4:00-4:50"
              />
        </TableRow>
      </HorizontalScrollView>
    </TableLayout>
  </ScrollView>
</TableLayout>

问题在于您将列大小设置为拉伸到其父项!它的父级是一个具有无限宽度的 Horizo​​ntalScrollView!因此,它无法设置其权重。

所以,您需要将 Table 放在 ScrollView(垂直)中,然后在 Table 中有 Horizo​​ntalScrollView。此外,您需要在更改后添加 height m width 属性。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/routine_heading"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/routine_header"
        android:textAppearance="?android:textAppearanceLarge"/>
    <ScrollView
        android:id="@+id/layout"
        android:layout_height="wrap_content"
        android:scrollbars="horizontal|vertical"
        android:layout_width="match_parent"
        android:layout_marginTop="8dip"
        android:scrollbarStyle="outsideOverlay"
        android:fillViewport="false">
            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/tlGridTable"
                android:stretchColumns="*">
                <HorizontalScrollView
                    android:id="@+id/horizontalView"
                    android:layout_height="wrap_content"
                    android:scrollbars="horizontal|vertical"
                    android:layout_width="wrap_content"
                    android:layout_marginTop="5dip">
                <TableRow
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1">
                    <TextView
                        android:background="@drawable/cell"
                        android:text=""
                        android:layout_height="match_parent"
                        android:layout_width="0dp"
                        android:layout_weight="1"/>
                    <TextView
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:background="@drawable/cell"
                        android:text="10-10:50"
                        android:padding="3dip"
                        android:layout_height="match_parent"
                        />
                    <TextView
                        android:text="10:50-11:40"
                        android:background="@drawable/cell"
                        android:padding="3dip"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        />
                    <TextView
                        android:background="@drawable/cell"
                        android:text="11:40-12:30"
                        android:padding="3dip"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="match_parent"
                        />
                    <TextView
                        android:background="@drawable/cell"
                        android:text="12:30-1:20"
                        android:padding="3dip"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="match_parent"
                        />
                    <TextView
                        android:background="@drawable/cell"
                        android:text="2:35-3:20"
                        android:padding="3dip"
                        android:layout_height="match_parent"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        />
                    <TextView
                        android:background="@drawable/cell"
                        android:text="3:20-4:00"
                        android:padding="3dip"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="match_parent"
                        />
                    <TextView
                        android:background="@drawable/cell"
                        android:text="4:00-4:50"
                        android:padding="3dip"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        />
                </TableRow>
                </HorizontalScrollView>
            </TableLayout>
    </ScrollView>
</LinearLayout>

Recommended to use a ListView/RecyclerView with a adapter for this kind of works.