Java Android Studio以编程方式创建板

Java Android Studio以编程方式创建板,java,android,layout,Java,Android,Layout,我目前正在尝试创建我的第一款平板安卓游戏,并努力调整我的布局。我的代码只创建了电路板的一行。我几乎读过每一篇文章,但找不到任何有用的东西 public class Nonogram extends AppCompatActivity { private Field field = new Field(10,10,75); private ImageView[][] board = new ImageView[field.getRowCount()][field.getColumnCount(

我目前正在尝试创建我的第一款平板安卓游戏,并努力调整我的布局。我的代码只创建了电路板的一行。我几乎读过每一篇文章,但找不到任何有用的东西

public class Nonogram extends AppCompatActivity {

private Field field = new Field(10,10,75);

private ImageView[][] board = new ImageView[field.getRowCount()][field.getColumnCount()];

Context context;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nonogram);
    context = this;
    draw();
}

private void draw(){

    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.nonogram);

    for(int row = 0; row < field.getRowCount(); row ++) {

        LinearLayout rowl = new LinearLayout(context);

        for (int column = 0; column < field.getColumnCount(); column++) {

            board[row][column] = new ImageView(context);

            if (field.getUserTiles()[row][column].getState() == TileState.FILLED) {
                board[row][column].setBackgroundResource(R.drawable.filled);

            } else if (field.getUserTiles()[row][column].getState() == TileState.EMPTY) {
                board[row][column].setBackgroundResource(R.drawable.empty);

            } else {
                board[row][column].setBackgroundResource(R.drawable.marked);
            }

            rowl.addView(board[row][column]);
        }
        relativeLayout.addView(rowl);
    }
}
公共类非图扩展了AppCompative活动{
专用字段=新字段(10,10,75);
私有ImageView[][]板=新ImageView[field.getRowCount()][field.getColumnCount()];
语境;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u非图形);
上下文=这个;
draw();
}
私人提款(){
RelativeLayout RelativeLayout=(RelativeLayout)findViewById(R.id.Nongram);
for(int row=0;row
XML只是相对布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nonogram"
android:background="@drawable/menu_background"
tools:context="pocketfun.org.pocketfun.nonogram.Nonogram">

</RelativeLayout>

改用LinearLayout作为根目录(不要忘记android:orientation=“vertical”):


并调整您的代码:

LinearLayout rootLayout = (LinearLayout) findViewById(R.id.nonogram);

for(int row = 0; row < field.getRowCount(); row ++) {
    [...]
    rootLayout.addView(row1);
}
LinearLayout rootLayout=(LinearLayout)findViewById(R.id.nonogram);
for(int row=0;row
对不起,我忘了。
LinearLayout rootLayout = (LinearLayout) findViewById(R.id.nonogram);

for(int row = 0; row < field.getRowCount(); row ++) {
    [...]
    rootLayout.addView(row1);
}