Android Studio以编程方式创建板

时间:2017-05-31 12:56:03

标签: java android layout

我目前正在尝试创建我的第一款Android游戏,并在我的布局上苦苦挣扎。我的代码只创建了一行电路板。我几乎读过每一篇文章,但找不到任何有用的东西。

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);
    }
}

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>

1 个答案:

答案 0 :(得分:1)

使用LinearLayout作为root(不要忘记android:orientation =“vertical”):

$transactions = Transaction::
    whereHas('subscription', function ($query) {
        $query->groupBy('subscriptions.user_id');
}, '>', 1)
    ->where('billing_date', '<=', Carbon::tomorrow())
    ->where('billing_date', '>=', Carbon::today())
    ->get();

调整代码:

<LinearLayout 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:orientation="vertical"
android:layout_height="match_parent"
android:id="@+id/nonogram"
android:background="@drawable/menu_background"
tools:context="pocketfun.org.pocketfun.nonogram.Nonogram">

</LinearLayout>