【问题标题】:Android what is the canonical way of creating a layout for tabular data?Android 为表格数据创建布局的规范方法是什么?
【发布时间】:2015-02-25 08:31:20
【问题描述】:

我正在尝试为如下所示的表格设计布局:

列是固定的,行可以动态添加/删除。

所以我尝试使用 TableLayout 来设计我的桌子的“骨架”。

<ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TableLayout
                android:id="@+id/tblSales"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="10dp"
                android:shrinkColumns="*"
                android:stretchColumns="*">
            <TableRow
                android:id="@+id/tblSalesCol"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:text="Inventory Info"
                    android:textSize="20sp"
                    android:layout_width="120dp"
                    />
                <TextView
                    android:text="Sold Price"
                    android:textSize="20sp"
                    android:layout_width="40dp"
                    />
                <TextView
                    android:text="Sold Qty"
                    android:textSize="20sp"
                    android:layout_width="5dp"
                    />

            </TableRow>
            <View style="@style/Divider"/>

            </TableLayout>
        </ScrollView>

并尝试为动态添加的行设计自定义表格布局

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
            <TextView
                android:id="@+id/tblrowSales_inv_name"
                android:textStyle="bold"
                android:textSize="15sp"
                android:textColor="#000000"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_height="30dp"
                android:layout_width="200dp"/>
            <TextView
                android:id="@+id/tblrowSales_detail"
                android:textStyle="bold"
                android:textColor="#000000"
                android:layout_height="20dp"
                android:layout_width="200dp"
                android:layout_below="@id/tblrowSales_inv_name"
                />
            <TextView
                android:id="@+id/tblrowSales_sold_price"
                android:textStyle="normal"
                android:textColor="#000000"
                android:layout_height="50dp"
                android:layout_width="100dp"
                android:layout_toRightOf="@id/tblrowSales_detail"/>
            <TextView
                android:id="@+id/tblrowSales_sold_qty"
                android:textStyle="normal"
                android:textColor="#000000"
                android:layout_height="50dp"
                android:layout_width="50dp"
                android:layout_toRightOf="@id/tblrowSales_sold_price"/>
    </RelativeLayout>
</TableRow>

主要问题是使用 relativelayout,我无法按比例设置字段的宽度(因为 layout_weight 仅支持 linearlayout)。

我也不认为行的字段宽度将与 TableLayout 中定义的列宽度同步。

总而言之,这种方法感觉很肮脏而且非常错误。

示例中创建表格的正确方法是什么?

【问题讨论】:

    标签: java android xml layout


    【解决方案1】:

    根据需要创建表的简单方法是使用GridLayout。通过这种方式,列宽适应内容和标题,行具有相同的列宽。

    例如:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <GridLayout
            android:id="@+id/tblSales"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:columnCount="3"
            android:padding="10dp" >
    
            <TextView
                android:text="Inventory Info"
                android:textSize="20sp" />
    
            <TextView
                android:text="Sold Price"
                android:textSize="20sp" />
    
            <TextView
                android:text="Sold Qty"
                android:textSize="20sp" />
    
            <TextView
                android:id="@+id/tblrowSales_inv_name"
                android:layout_width="200dp"
                android:layout_height="30dp"
                android:text="BackPAck"
                android:textColor="#000000"
                android:textSize="15sp"
                android:textStyle="bold" />
    
            <TextView
                android:id="@+id/tblrowSales_sold_price"
                android:layout_gravity="center"
                android:layout_rowSpan="2"
                android:text="33.50$"
                android:textColor="#000000"
                android:textStyle="normal" />
    
            <TextView
                android:id="@+id/tblrowSales_sold_qty"
                android:layout_gravity="center"
                android:layout_rowSpan="2"
                android:text="1"
                android:textColor="#000000"
                android:textStyle="normal" />
    
            <TextView
                android:id="@+id/tblrowSales_detail"
                android:layout_width="200dp"
                android:layout_height="20dp"
                android:text="Sales Regular"
                android:textColor="#000000"
                android:textStyle="bold" />
        </GridLayout>
    
    </ScrollView>
    

    【讨论】:

    • @l46kok 有用吗?
    【解决方案2】:

    您是否尝试过以编程方式添加行,而不是加载 xml?这只是一个想法,但您可以在 xml 中制作标题,然后在您的 onCreate 中添加带有您想要的 LayoutParams 的 TextViews。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 2011-01-07
      • 1970-01-01
      相关资源
      最近更新 更多