2017-09-04 78 views
0

在建筑物中,井字棋网格布局占据上层,而Linearlayout占据网格布局正下方的下层。如果任何玩家赢得比赛,线性布局应该出现。在线性布局中,获胜的玩家和将游戏设置为起始位置的按钮。所以最初我在游戏结束时将其设置为不可见,线性布局回到了网格布局的背面。如何在网格布局之上制作线性布局?如何优先考虑顶部布局?

MainActivity.java

package com.example.achyu.tictactao; 

import android.graphics.Color; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 

//0=yellow 1=red 
int currentPlayer=0; 

int[] isItDone={2, 2, 2, 2, 2, 2, 2, 2, 2}; 

//2=yellow 1=red 
int[] whoWon=new int[9]; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

public void currentCounter(View view){ 

    ImageView counter= (ImageView) view; 

    int tap=Integer.parseInt(counter.getTag().toString()); 

    if(isItDone[tap]==2){ 

     counter.setTranslationY(-1000f); 
     if(currentPlayer==0){ 

      counter.setImageResource(R.drawable.yellow); 
      currentPlayer=1; 
      whoWon[tap]=2; 


      if((whoWon[0]==2&&whoWon[1]==2&&whoWon[2]==2)||(whoWon[3]==2&&whoWon[4]==2&&whoWon[5]==2)|| 
        (whoWon[6]==2&&whoWon[7]==2&&whoWon[8]==2)||(whoWon[0]==2&&whoWon[3]==2&&whoWon[6]==2)|| 
        (whoWon[1]==2&&whoWon[4]==2&&whoWon[7]==2)||(whoWon[2]==2&&whoWon[5]==2&&whoWon[8]==2)|| 
        (whoWon[0]==2&&whoWon[4]==2&&whoWon[8]==2)||(whoWon[2]==2&&whoWon[4]==2&&whoWon[6]==2)) 
      { 
       Toast.makeText(this,"Yellow Has Won",Toast.LENGTH_LONG).show(); 

       LinearLayout layout = (LinearLayout)findViewById(R.id.linearlayout); 
       layout.setVisibility(View.VISIBLE); 
       TextView winnerText= (TextView) findViewById(R.id.winner); 
       winnerText.setText("Yello Won"); 
       winnerText.setTextColor(Color.WHITE); 

      } 
     } 
     else 
     { 
      counter.setImageResource(R.drawable.red); 
      currentPlayer=0; 

      whoWon[tap]=1; 

      if((whoWon[0]==1&&whoWon[1]==1&&whoWon[2]==1)||(whoWon[3]==1&&whoWon[4]==1&&whoWon[5]==1)|| 
        (whoWon[6]==1&&whoWon[7]==1&&whoWon[8]==1)||(whoWon[0]==1&&whoWon[3]==1&&whoWon[6]==1)|| 
        (whoWon[1]==1&&whoWon[4]==1&&whoWon[7]==1)||(whoWon[2]==1&&whoWon[5]==1&&whoWon[8]==1)|| 
        (whoWon[0]==1&&whoWon[4]==1&&whoWon[8]==1)||(whoWon[2]==1&&whoWon[4]==1&&whoWon[6]==1)) 
      { 
       Toast.makeText(this,"Red Has Won",Toast.LENGTH_LONG).show(); 

       LinearLayout layout = (LinearLayout)findViewById(R.id.linearlayout); 
       layout.setVisibility(View.VISIBLE); 
       TextView winnerText= (TextView) findViewById(R.id.winner); 
       winnerText.setText("Red Won"); 
       winnerText.setTextColor(Color.WHITE); 
      } 
     } 
     counter.animate().translationYBy(1000f).setDuration(300); 
     isItDone[tap]=3; 


    } 
} 
} 

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.example.achyu.tictactao.MainActivity"> 

<RelativeLayout 
    android:layout_width="395dp" 
    android:layout_height="587dp" 
    tools:layout_editor_absoluteX="8dp" 
    tools:layout_editor_absoluteY="8dp"> 

    <LinearLayout 
     android:id="@+id/linearlayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:background="@drawable/orange" 
     android:orientation="vertical" 
     android:padding="60dp" 
     android:visibility="invisible"> 

     <TextView 
      android:id="@+id/winner" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      android:text="TextView" 
      android:textSize="40sp" 
      tools:textColor="#ffffff" /> 

     <Button 
      android:id="@+id/playagain" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 

    </LinearLayout> 

    <GridLayout 
     android:layout_width="360dp" 
     android:layout_height="360dp" 
     android:background="@drawable/board" 
     android:columnCount="3" 
     android:rowCount="3" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true"> 

     <ImageView 
      android:id="@+id/imageView0" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:layout_column="0" 
      android:layout_marginLeft="10dp" 
      android:tag="0" 
      android:layout_row="0" 
      android:onClick="currentCounter" /> 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:layout_column="1" 
      android:layout_marginLeft="20dp" 
      android:tag="1" 
      android:layout_row="0" 
      android:onClick="currentCounter" /> 

     <ImageView 
      android:id="@+id/imageView2" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:layout_column="2" 
      android:layout_marginLeft="25dp" 
      android:tag="2" 
      android:layout_row="0" 
      android:onClick="currentCounter" /> 

     <ImageView 
      android:id="@+id/imageView3" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:layout_column="0" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="30dp" 
      android:tag="3" 
      android:layout_row="1" 
      android:onClick="currentCounter" /> 

     <ImageView 
      android:id="@+id/imageView4" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:layout_column="1" 
      android:layout_marginLeft="20dp" 
      android:layout_marginTop="30dp" 
      android:tag="4" 
      android:layout_row="1" 
      android:onClick="currentCounter" /> 

     <ImageView 
      android:id="@+id/imageView5" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:layout_column="2" 
      android:layout_marginLeft="25dp" 
      android:layout_marginTop="30dp" 
      android:tag="5" 
      android:layout_row="1" 
      android:onClick="currentCounter" /> 

     <ImageView 
      android:id="@+id/imageView6" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:layout_column="0" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="30dp" 
      android:tag="6" 
      android:layout_row="2" 
      android:onClick="currentCounter" /> 

     <ImageView 
      android:id="@+id/imageView7" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:layout_column="1" 
      android:layout_marginLeft="20dp" 
      android:layout_marginTop="30dp" 
      android:tag="7" 
      android:layout_row="2" 
      android:onClick="currentCounter" /> 

     <ImageView 
      android:id="@+id/imageView8" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:layout_column="2" 
      android:layout_marginLeft="25dp" 
      android:layout_marginTop="30dp" 
      android:tag="8" 
      android:layout_row="2" 
      android:onClick="currentCounter" /> 
    </GridLayout> 

</RelativeLayout> 

+0

嵌套布局是不好的表演,万一你关心你的用户体验。 –

回答

0

只要把你的网格布局下方的线性布局的相对布局

+0

谢谢你,是的,它的工作问题表明我是一个noob。这是真的。 –

+0

没问题,你能接受我的答案plz – Vodet