TableRow переходит на 0 высоту после нажатия каждого ребенка - PullRequest
0 голосов
/ 20 сентября 2018

Здравствуйте. В настоящее время я пытаюсь запрограммировать собственный тральщик и столкнулся с проблемой, которую не мог решить.Я использую TableLayout для игрового поля.Каждая строка этого макета содержит несколько кнопок.Но когда нажимается каждая кнопка строки, она уменьшает свою собственную высоту до 0. Я не мог понять проблему и хотел обратиться за помощью сюда.

Я получил несколько скриншотов из инспектора макета:

Начальная точка

Все хорошо

Если я сейчас нажму последнее поле первой строки, это произойдет

Код для сгенерированных по типу строк и кнопок:

 public void setupGame(PM x) {
    Tile[][] field = x.getField();
    TableLayout table = findViewById(R.id.field);
    TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 0);
    rowParams.weight = 1f;
    TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(0, TableLayout.LayoutParams.MATCH_PARENT);
    buttonParams.weight = 1f;
    for (int rows = 0; rows < field.length; rows++) {
        TableRow tableRow = new TableRow(getApplicationContext());
        table.addView(tableRow);
        tableRow.setLayoutParams(rowParams);
        for (int columns = 0; columns < field[rows].length; columns++) {
            Button button = new Button(getApplicationContext());
            tableRow.addView(button);
            button.setLayoutParams(buttonParams);
            button.setBackground(getDrawable(R.drawable.grass));
            field[rows][columns].setButton(button);
            setClickListener(button, field[rows][columns]);
        }
    }
}

private void setClickListener(Button button, Tile tile) {
    button.setOnLongClickListener(v -> {
        if (!tile.visible) {
            button.setBackground(getDrawable(R.drawable.flagongrass));
        }
        return true;
    });
    if (tile.mine) {                                                                              // when you click on a mine -> Game over
        button.setOnClickListener(v -> {
            if (!tile.visible) {
                button.setBackground(getDrawable(R.drawable.bomb));
            }
            tile.visible = true;
            //gameLost();
        });
    } else if (tile.number == 0) {                                                                  // when you click on an empty tile -> Reveal empty tiles
        button.setOnClickListener(v -> {
            if (!tile.visible) {
                button.setVisibility(View.INVISIBLE);
                checkForNearbyEmpty(tile);
            }
            tile.visible = true;
        });
    } else {                                                                                       // when you click on a numbered tile -> show number
        button.setOnClickListener(v -> {
            if (!tile.visible) {
                button.setBackgroundColor(Color.TRANSPARENT);
                button.setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM);
                button.setText(String.valueOf(tile.number));
            }
            tile.visible = true;
        });
    }
}

Код действия xml:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:background="@drawable/grass"
tools:context=".Gamescreen">


<TableLayout
    android:id="@+id/field"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/dirt3"
    app:layout_constraintBottom_toTopOf="@+id/horizontalGuideline"
    app:layout_constraintLeft_toRightOf="@id/verticalGuideline2"
    app:layout_constraintRight_toLeftOf="@id/verticalGuideline"
    app:layout_constraintTop_toBottomOf="@+id/horizontalGuideline2">

</TableLayout>

И класс Tile:

public class Tile {
public boolean mine;
public boolean visible;
public int number;
private Button button;
private int x;
private int y;

public Tile(){
    this.mine = false;
    this.visible = false;
    this.number = 0;
}
public Tile(int x, int y){
    this.x = x;
    this.y = y;
}

// Setter

public void setButton(Button b){
    this.button = b;
}

// Getter

public Button getButton(){
    return this.button;
}

public int getX() {
    return this.x;
}

public int getY() {
    return this.y;
}

@Override
public String toString(){
    if(number == 0 && !mine){
        return " ";
    }
    return String.valueOf(number);
}
}

Это мой первый пост, надеюсь, все понятно.

...