开发者

How would you draw a matrix in Android Development?

The size of the matrix would be input by the user with variables h, w.

Given this how would I create a matrix of that size on the screen all开发者_JS百科owing the user to enter in values inside the matrix.


probably by generateing a http://developer.android.com/guide/topics/ui/layout-objects.html#tablelayout in a http://developer.android.com/reference/android/view/ViewGroup.html


The code above doesn't quite work but it's almost there. I had to change the adds to addView.

import android.widget.TableRow.LayoutParams;
import android.widget.TableRow;
import android.widget.EditText;
import android.widget.TableLayout;

public void onCreate(Bundle b) {
    TableLayout table = new TableLayout(this);
    for (int i = 0; i < rows; i++) {
        TableRow row = new TableRow(this);
        for (int j = 0; j < columns; j++) {
            EditText cell = new EditText(this);
            cell.setText("(" + i + ", " + j + ")");
            row.addView(cell);
        }
        table.addView(row new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT)););
    }
    setContentView(table);
}


Try using TableLayout and TableRow.

I haven't used them, but I'm guessing your activity code would look like this:

public void onCreate(Bundle b) {
    TableLayout table = new TableLayout(this);
    for (int i = 0; i < rows; i++) {
        TableRow row = new TableRow(this);
        for (int j = 0; j < columns; j++) {
            EditText cell = new EditText(this);
            cell.setText("(" + i + ", " + j + ")");
            row.add(cell);
        }
        table.add(row);
    }
    setContentView(table);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜