[英]Android what is the canonical way of creating a layout for tabular data?
我正在尝试为表格设计一个布局,如下所示:
列是固定的,行可以动态添加/删除。
因此,我尝试使用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中定义的列宽度同步。
总而言之,这种方法感觉很脏而且非常错误。
创建示例中显示的表格的正确方法是什么?
根据需要创建表的简单方法是使用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>
您是否尝试过以编程方式添加行,而不是加载xml? 这只是一个主意,但是您可以在xml中创建标题,然后在onCreate中添加具有所需LayoutParams的TextView。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.