Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android为表格数据创建布局的标准方法是什么?_Java_Android_Xml_Layout - Fatal编程技术网

Java Android为表格数据创建布局的标准方法是什么?

Java Android为表格数据创建布局的标准方法是什么?,java,android,xml,layout,Java,Android,Xml,Layout,我正在尝试为一个表设计一个布局,如下所示: 列是固定的,行可以动态添加/删除 所以我试着用TableLayout来设计我桌子的“骨架” <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TableLayout android:id="@+id/tbl

我正在尝试为一个表设计一个布局,如下所示:

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

所以我试着用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>

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

<?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,我无法按比例设置字段的宽度(因为只有linearlayout支持布局权重)

我还认为行的字段宽度不会与TableLayout中定义的列宽同步

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


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

您是否尝试过以编程方式添加行,而不是加载xml?这只是一个想法,但您可以在xml中创建标题,然后在onCreate中添加包含所需布局参数的文本视图。

创建所需表格的简单方法是使用。以这种方式,列宽适应于内容,标题和行具有相同的列宽

例如:

<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它有用吗?