之前我們實作的部分都還沒進入主要功能,這篇開始,我們將進入最主要的玩家介面,最終會實作出2個玩家(player1、player2)的介面,兩個介面一樣,這裡只實作一個做為示範
這邊先附上layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="#ab7557"
tools:context=".player1.Player1ChessGame">
<TextView
android:id="@+id/textView13"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/black"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_room_number"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center|right"
android:text="房號:"
android:textColor="@color/white"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintEnd_toStartOf="@+id/guideline5"
app:layout_constraintStart_toEndOf="@+id/btn_back"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_show_room_number"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center_vertical"
android:text=""
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintEnd_toStartOf="@+id/guideline38"
app:layout_constraintStart_toEndOf="@+id/tv_room_number"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_back"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/redbackground_blackline"
android:gravity="center"
android:text="退出"
android:textColor="@color/white"
android:textStyle="bold"
app:backgroundTint="@null"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/guideline9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline8">
<androidx.gridlayout.widget.GridLayout
android:id="@+id/grid_playerChessboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true">
</androidx.gridlayout.widget.GridLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toStartOf="@+id/guideline7"
app:layout_constraintStart_toStartOf="@+id/guideline6"
app:layout_constraintTop_toTopOf="@+id/guideline">
<androidx.gridlayout.widget.GridLayout
android:id="@+id/grid_playerChessmanSelect"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true">
</androidx.gridlayout.widget.GridLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toStartOf="@+id/guideline7"
app:layout_constraintStart_toStartOf="@+id/guideline6"
app:layout_constraintTop_toTopOf="@+id/guideline">
<androidx.gridlayout.widget.GridLayout
android:id="@+id/grid_playerChessman"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true">
</androidx.gridlayout.widget.GridLayout>
</RelativeLayout>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline76"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.175" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.25" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.7" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.75" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.45" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.75" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.9" />
<TextView
android:id="@+id/tv_message"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/brownline"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:text=""
android:textColor="#1e2371"
app:layout_constraintBottom_toTopOf="@+id/guideline76"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline3" />
<TextView
android:id="@+id/tv_systemMessage"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/brownline"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:text="系統訊息:"
android:textColor="@color/white"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline8"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline76" />
<Button
android:id="@+id/btn_setting"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/redbackground_blackline"
android:text="設置"
android:textColor="@color/white"
android:textStyle="bold"
app:backgroundTint="@null"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline38"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.64" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/brownline"
app:layout_constraintBottom_toTopOf="@+id/guideline8"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/brownline"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline9" />
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/guideline102"
app:layout_constraintEnd_toStartOf="@+id/guideline88"
app:layout_constraintStart_toStartOf="@+id/guideline54"
app:layout_constraintTop_toTopOf="@+id/guideline103">
<TextView
android:id="@+id/tv_moveData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="@color/white" />
</ScrollView>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline54"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.05" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline88"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.95" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline103"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.77" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline102"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.98" />
</androidx.constraintlayout.widget.ConstraintLayout>
整個介面設計分為幾個部分:
GridLayout部分需要在build.gradle加上maven:
implementation 'androidx.gridlayout:gridlayout:1.0.0'
這樣我們就實作了主要下棋功能的介面,player2也是照同樣的方式設計