您好,我目前正在尝试编写自己的扫雷器,遇到一个我无法解决的问题。我将TableLayout用于游戏领域。此布局的每一行都包含多个按钮。但是,当单击一行中的每个按钮时,该行会将其自身的高度减小到0。我无法找出问题,想在这里寻求帮助。
我从布局检查器中获得了一些屏幕截图:
If I now click the last field of the first row this happens
以语法方式生成的行和按钮的代码:
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);
}
}
这是我的第一篇文章,我希望它的一切都可以理解。