1

我希望我的二维数组显示为矩阵格式。对于那个布局是合适的?我可以举个例子吗?

4

1 回答 1

1

简单的答案是在里面使用 aTableLayoutTextViews。

但是如果你想显示一个大数组,你应该让矩阵在两个方向上都可以滚动。EditText下面是填充矩阵的代码,该矩阵显示为行内的一系列s,在 a 内TableLayout。使用TableLayout两个 ScrollView 可以在两个方向上滚动。根据您的需要调整代码。tableTableLayout带有 id 的tableLayout1

private void fillTable(final int n, final double[][] matrix, TableLayout table) {
table.removeAllViews();
for (int i = 0; i < n; i++) {
    TableRow row = new TableRow(MainActivity.this);
    row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

    for (int j = 0; j < n; j++) {
        EditText edit = new EditText(OutputActivity.this);
        edit.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_NUMBER_FLAG_SIGNED);
        edit.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

        edit.setText(Double.toString(matrix[i][j]));

        edit.setKeyListener(null);
        row.addView(edit);
    }
    table.addView(row);
}
}

布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

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

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="horizontal" >

                    <TableLayout
                        android:id="@+id/tableLayout1"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent" >
                    </TableLayout>
                </LinearLayout>
            </HorizontalScrollView>
        </LinearLayout>
    </ScrollView>
</LinearLayout>
于 2013-04-14T10:45:37.173 回答