Android 以矩阵格式显示二维阵列

Android 以矩阵格式显示二维阵列,android,android-layout,Android,Android Layout,我希望我的2D数组显示为矩阵格式。哪种布局合适?我能举个例子吗?简单的答案是使用TableLayout,里面有TextViews 但是如果你想显示一个大的数组,你应该使矩阵在两个方向上都可以滚动。下面是填充矩阵的代码,该矩阵在表格布局中的行内显示为一系列EditTexts。TableLayout可以使用双滚动视图在两个方向上滚动。使代码适应您的需要table是id为tableLayout1的TableLayout private void fillTable(final int n, final

我希望我的2D数组显示为矩阵格式。哪种布局合适?我能举个例子吗?

简单的答案是使用
TableLayout
,里面有
TextView
s

但是如果你想显示一个大的数组,你应该使矩阵在两个方向上都可以滚动。下面是填充矩阵的代码,该矩阵在
表格布局中的行内显示为一系列
EditText
s。
TableLayout
可以使用双滚动视图在两个方向上滚动。使代码适应您的需要
table
是id为
tableLayout1
TableLayout

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);
}
}
private void fillTable(最终int n,最终double[][]矩阵,TableLayout表){
table.removeAllViews();
对于(int i=0;i
布局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>

简单的答案是使用
表格布局
,其中包含
文本视图
s

但是如果你想显示一个大的数组,你应该使矩阵在两个方向上都可以滚动。下面是填充矩阵的代码,该矩阵在
表格布局中的行内显示为一系列
EditText
s。
TableLayout
可以使用双滚动视图在两个方向上滚动。使代码适应您的需要
table
是id为
tableLayout1
TableLayout

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);
}
}
private void fillTable(最终int n,最终double[][]矩阵,TableLayout表){
table.removeAllViews();
对于(int i=0;i
布局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>


在查看了可用的android布局后,您认为哪种布局合适?我认为是表格布局。bt我不太确定这是y,我在看了安卓的可用布局后问了他,你认为哪一个是合适的?我认为是表格布局。我不太确定我问的是y