0
我正在使用Android 2.3.3版本並在其上製作一個tic tac toe遊戲。在設計佈局時,我遇到了佈局對象的問題,並且遊戲的遊戲狀態也有問題。 任何幫助將是偉大的!我的Android佈局設計在仿真器上運行時不在同一位置,並顯示它們位置不正確
下面的圖片是我的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"
tools:context="com.sherzod.ion.tictactoeandroid.MainActivity">
<GridLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="46dp"
android:layout_marginEnd="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginStart="1dp"
android:layout_marginTop="46dp"
android:background="@drawable/board"
android:columnCount="3"
android:rowCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1">
<ImageView
android:id="@+id/imageView"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="0"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_row="0"
android:onClick="dropIn"
android:tag="0" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="1"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_row="0"
android:onClick="dropIn"
android:tag="1"/>
<ImageView
android:id="@+id/imageView6"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="2"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_row="0"
android:onClick="dropIn"
android:tag="2"/>
<ImageView
android:id="@+id/imageView9"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="0"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="40dp"
android:layout_row="1"
android:onClick="dropIn"
android:tag="3"/>
<ImageView
android:id="@+id/imageView10"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="1"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="40dp"
android:layout_row="1"
android:onClick="dropIn"
android:tag="4"
tools:ignore="RtlHardcoded" />
<ImageView
android:id="@+id/imageView11"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="2"
android:layout_marginLeft="20dp"
android:layout_marginTop="40dp"
android:layout_row="1"
android:onClick="dropIn"
android:tag="5" />
<ImageView
android:id="@+id/imageView12"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="50dp"
android:layout_row="2"
android:onClick="dropIn"
android:tag="6"/>
<ImageView
android:id="@+id/imageView14"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="2"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:layout_row="2"
android:onClick="dropIn"
android:tag="7"/>
<ImageView
android:id="@+id/imageView13"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="1"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:layout_row="2"
android:onClick="dropIn"
android:tag="8"/>
</GridLayout>
</android.support.constraint.ConstraintLayout>
這是我的主要活動文件:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
// 0 = yellow ; 1 = red;
int activePlayer = 0;
// ** 2 means unplayed
int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
public void dropIn(View view) {
ImageView counter = (ImageView) view;
System.out.println(counter.getTag().toString());
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if(gameState[tappedCounter] == 2) {
gameState[tappedCounter] = activePlayer;
}
counter.setTranslationY(-1000f);
if(activePlayer == 0) {
counter.setImageResource(R.drawable.yellow);
activePlayer = 1;
}else {
counter.setImageResource(R.drawable.red);
activePlayer = 0;
}
counter.animate().translationYBy(1000f).rotation(360).setDuration(200);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
遺憾,但是當我在模擬器上運行它,它是一切搞砸! – Sherzod
我也在模擬器上進行了檢查,但沒有使用'GridLayout'背景。你可以檢查沒有'GridLayout'背景嗎? –
你是否知道爲什麼拖放功能不按預期定位項目,或者我應該進行任何配置以使佈局設計視圖在仿真器上運行時看起來相同? – Sherzod