我有以下代码:

<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android: {code is required here}="2">

<TableRow>
    <TextView
        android:id="@+id/accelerometer_label"
        android: {code is required here}="1"
        android:text="Accelerometer"
        android:textSize="9pt"
        android:padding="3dip" />
</TableRow>

我试图将数据显示在三列中,知道在{code is here这里需要什么代码}部分中需要输入什么代码?

最佳答案

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:shrinkColumns="*"
    android:stretchColumns="*">

    <!-- Row 1 with single column -->
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="3"
            android:background="#b0b0b0"
            android:padding="18dip"
            android:text="@string/row_1"
            android:textColor="#000"
            android:textSize="18sp" />


    </TableRow>

    <!-- Row 2 with 3 columns -->
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_weight="1"
            android:background="#dcdcdc"
            android:gravity="center"
            android:padding="20dip"
            android:text="@string/row_2_column_1"
            android:textColor="#000000" />

        <TextView
            android:layout_weight="1"
            android:background="#d3d3d3"
            android:gravity="center"
            android:padding="20dip"
            android:text="@string/row_2_column_2"
            android:textColor="#000000" />

        <TextView
            android:layout_weight="1"
            android:background="#cac9c9"
            android:gravity="center"
            android:padding="20dip"
            android:text="@string/row_2_column_3"
            android:textColor="#000000" />

    </TableRow>

    <!-- Row 3 with 2 columns -->
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_weight="1"
            android:background="#b0b0b0"
            android:gravity="center"
            android:padding="18dip"
            android:text="@string/row_3_column_1"
            android:textColor="#000000" />

        <TextView
            android:layout_weight="1"
            android:background="#a09f9f"
            android:gravity="center"
            android:padding="18dip"
            android:text="@string/row_3_column_2"
            android:textColor="#000000" />
    </TableRow>

 </TableLayout>

关于android - 如何在Android TableLayout中设置列数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37238250/

10-10 18:08