似乎我无法将startingPlayerLayout 放在其他视图的前面。我尝试了多种解决方案,最后一个是下面的代码,结果使 LinearLayout 可见但在后台...(这是我关于 Stack Overflow 的第一个问题,请原谅任何错误)谢谢大家
public class MainActivity extends AppCompatActivity {
boolean gameFinished = false;
// Player 1 = Black, Player 2 = Red
int playerActive;
int[] gameState = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int[][] winningPositions = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};
TextView gameInfoField;
Button playAgainButton;
LinearLayout startingPlayerLayout;
ConstraintLayout baseLayout;
// The actual gameplay follows:
public void player1Starts(View view) {
playerActive = 1;
startingPlayerLayout.setVisibility(View.INVISIBLE);
}
public void player2Starts(View view) {
playerActive = 2;
startingPlayerLayout.setVisibility(View.INVISIBLE);
}
public void playCounter(View view) {
ImageView counter = (ImageView) view;
int squarePlayed = Integer.parseInt(counter.getTag().toString());
gameInfoField = (TextView) findViewById(R.id.gameInfoField);
if (!gameFinished) {
if (gameState[squarePlayed] == 0) {
counter.setTranslationY(-1000f);
if (playerActive == 1) {
counter.setImageResource(R.drawable.token_black);
gameState[squarePlayed] = 1;
playerActive = 2;
gameInfoField.setText("Tocca al ROSSO");
} else {
counter.setImageResource(R.drawable.token_red);
gameState[squarePlayed] = 2;
playerActive = 1;
gameInfoField.setText("Tocca al NERO");
}
counter.animate().translationYBy(1000f);
} else if (gameState[squarePlayed] == 0 && !gameFinished) {
Toast.makeText(getApplicationContext(), "La casella è già occupata", Toast.LENGTH_SHORT).show();
}
// Need to check if there's a winner, or if it's a draw:
for (int[] winningPosition : winningPositions) {
playAgainButton = (Button) findViewById(R.id.playAgainButton);
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] != 0) {
gameFinished = true;
if (gameState[winningPosition[0]] == 1) {
gameInfoField.setText("IL NERO VINCE!");
} else {
gameInfoField.setText("IL ROSSO VINCE!");
}
gameFinished = true;
playAgainButton.setVisibility(View.VISIBLE);
}
boolean fullBoard = true;
for (int square : gameState) {
if (square == 0) {
fullBoard = false;
}
}
if (fullBoard && !gameFinished) {
gameInfoField.setText("Pareggio");
playAgainButton.setVisibility(View.VISIBLE);
}
}
} else {
Toast.makeText(getApplicationContext(), "Abbiamo già un vincitore!", Toast.LENGTH_SHORT).show();
}
}
// And finally, we need to be able to play again if we want to:
public void playAgain(View view) {
gameFinished = false;
for (int i = 0; i < gameState.length; i++) {
gameState[i] = 0;
}
GridLayout board = (GridLayout) findViewById(R.id.board);
for (int i = 0; i < board.getChildCount(); i++) {
((ImageView) board.getChildAt(i)).setImageResource(0);
}
playAgainButton.setVisibility(View.INVISIBLE);
baseLayout.bringChildToFront(startingPlayerLayout);
startingPlayerLayout.setVisibility(View.VISIBLE);
if (playerActive == 1) {
gameInfoField.setText("Tocca al NERO");
}
else {
gameInfoField.setText("Tocca al ROSSO");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
baseLayout = (ConstraintLayout) findViewById(R.id.baseLayout);
startingPlayerLayout = (LinearLayout) findViewById(R.id.startingPlayerLayout);
baseLayout.bringChildToFront(startingPlayerLayout);
startingPlayerLayout.setVisibility(View.VISIBLE);
}
}
这是 XML 文件(我试图在它的末尾移动 LinearLayout,但问题仍然存在):
<?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:id="@+id/baseLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright"
tools:context="com.williamzannoni.tris.MainActivity">
<LinearLayout
android:id="@+id/startingPlayerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_orange_light"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/startingPlayerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CHI COMINCIA?"
android:textSize="25sp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="5dp" />
<Button
android:id="@+id/startingBlackButton"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:background="?android:attr/colorButtonNormal"
android:onClick="player1Starts"
android:text="NERO" />
<Button
android:id="@+id/startingRedButton"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:onClick="player2Starts"
android:text="ROSSO" />
</LinearLayout>
<GridLayout
android:id="@+id/board"
android:layout_width="368dp"
android:layout_height="368dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/board_grey"
android:columnCount="3"
android:elevation="1dp"
android:rowCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.496">
<ImageView
android:id="@+id/imageView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="15dp"
android:onClick="playCounter"
android:tag="0" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="22dp"
android:layout_marginTop="15dp"
android:onClick="playCounter"
android:tag="1" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="22dp"
android:layout_marginTop="15dp"
android:onClick="playCounter"
android:tag="2" />
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="0"
android:layout_marginLeft="12dp"
android:layout_marginTop="22dp"
android:layout_row="1"
android:onClick="playCounter"
android:tag="3" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="22dp"
android:layout_marginTop="22dp"
android:onClick="playCounter"
android:tag="4" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="22dp"
android:layout_marginTop="22dp"
android:onClick="playCounter"
android:tag="5" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="0"
android:layout_marginLeft="12dp"
android:layout_marginTop="22dp"
android:layout_row="2"
android:onClick="playCounter"
android:tag="6" />
<ImageView
android:id="@+id/imageView9"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="22dp"
android:layout_marginTop="22dp"
android:onClick="playCounter"
android:tag="7" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="2"
android:layout_marginLeft="22dp"
android:layout_marginTop="22dp"
android:layout_row="2"
android:onClick="playCounter"
android:tag="8" />
</GridLayout>
<TextView
android:id="@+id/gameInfoField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@android:color/holo_blue_bright"
android:padding="10dp"
android:text="Tocca al NERO"
android:textSize="25sp"
app:layout_constraintBottom_toTopOf="@+id/board"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.727" />
<Button
android:id="@+id/playAgainButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:background="@android:color/holo_blue_dark"
android:onClick="playAgain"
android:padding="10dp"
android:text="GIOCA ANCORA"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>