如何交换表中的列?

时间:2016-07-22 08:02:23

标签: android android-layout

如何更改蓝色列的位置,比如在最右侧制作蓝色列?

请参阅this screenshot

java -jar vertxtest.jar -cluster -cluster-host <your.ip> -cluster-port 5800

我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:id="@+id/fillable_area">

    <TableLayout
        android:id="@+id/table_header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_horizontal"
            >
             <TableLayout
                android:id="@+id/fixed_column"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

             <HorizontalScrollView
               android:id="@+id/scroller"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <TableLayout
                    android:id="@+id/scrollable_part"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"/>
            </HorizontalScrollView>


        </LinearLayout>
    </ScrollView>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我认为你需要遍历所有TableRows并交换他们的观点。例如,要交换第一列和第二列:

        TableLayout table = (TableLayout)findViewById(R.id.scrollable_part);
        int sourceColumnIndex = 0;
        int targetColumnIndex = 1;
        for (int i = 0; i < table.getChildCount(); i++)
        {
            TableRow tr = (TableRow)table.getChildAt(i);
            View v = tr.getChildAt(sourceColumnIndex);
            tr.removeViewAt(sourceColumnIndex);
            tr.addView(v, targetColumnIndex);
        }

编辑: 我在自己的小项目中检查了它。我现在看到,在你的项目中,交换的视图有不同的父级。我改变了你的代码,即使对于固定部分也添加'TableRow':

    row = new TableRow(this);
    row.setLayoutParams(wrapWrapTableRowParams);
    row.setGravity(Gravity.CENTER);
    row.addView(fixedView);
    fixedColumn.addView(row);

之后,交换例程是:

                TableLayout fixedColumnTable = (TableLayout) findViewById(R.id.fixed_column);
                TableLayout table = (TableLayout)findViewById(R.id.scrollable_part);
                if (table.getChildCount() == fixedColumnTable.getChildCount())
                {
                    int fixedColumnIndex = 0;
                    int targetColumnIndex = 2;
                    for (int i = 0; i < table.getChildCount(); i++)
                    {
                        TableRow fixedTR = (TableRow)fixedColumnTable.getChildAt(i);
                        View fixedView = fixedTR.getChildAt(fixedColumnIndex);

                        TableRow targetTR = (TableRow)table.getChildAt(i);
                        View targetView = targetTR.getChildAt(targetColumnIndex);

                        fixedTR.removeViewAt(fixedColumnIndex);
                        targetTR.removeViewAt(targetColumnIndex);
                        fixedTR.addView(targetView, fixedColumnIndex);
                        targetTR.addView(fixedView, targetColumnIndex);
                    }
                }