2016-11-17 104 views
1

我想提出一個井字棋遊戲,我需要讓我的按鈕相同的寬度和高度。具有相同寬度和高度的Android按鈕?

這是我的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:gravity="center_horizontal" ...> 

    <TextView 
     android:textSize="30dp" 
     android:textStyle="bold" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Tic-Tac-Toe" /> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <Button 
      android:id="@+id/cell_00" 
      android:layout_weight="1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     <Button 
      android:id="@+id/cell_10" 
      android:layout_weight="1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     <Button 
      android:id="@+id/cell_20" 
      android:layout_weight="1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

    <!--previous linear layout repeats twice more--> 

</LinearLayout> 

這是我的活動:

// imports 
import android.view.ViewGroup; 
public class TicTacToeActivity extends AppCompatActivity { 

    private static final int SIZE = 3; 

    @BindView(R.id.game_feedback) 
    TextView gameFeedback; 

    private Button[][] grid = new Button[SIZE][SIZE]; 
    private int[][] cell_ids = { 
      {R.id.cell_00, R.id.cell_01, R.id.cell_02}, 
      {R.id.cell_10, R.id.cell_11, R.id.cell_12}, 
      {R.id.cell_20, R.id.cell_21, R.id.cell_22} 
    }; 

    private Button getButtonById(int id) { 
     return (Button) findViewById(id); 
    } 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_tic_tac_toe); 
     ButterKnife.bind(this); 
     gameFeedback.setVisibility(View.INVISIBLE); 
     loadGridButtons(); 

     Button button = (Button) findViewById(R.id.cell_00); 
     int size = button.getLayoutParams().width; 
     button.setLayoutParams(new ViewGroup.LayoutParams(size, size)); 
    } 

    private void loadGridButtons() { 
     for (int i = 0; i < SIZE; i++) { 
      for (int j = 0; j < SIZE; j++) { 
       grid[i][i] = getButtonById(cell_ids[i][j]); 
      } 
     } 
    } 
} 

不過,我收到以下錯誤崩潰我的應用程序。

致命異常:主 工藝:com.mathsistor.m.tictactoe,PID:20927 java.lang.ClassCastException:android.view.ViewGroup $的LayoutParams 不能轉換到android.widget.LinearLayout $的LayoutParams

回答

1

代替button.setLayoutParams(new ViewGroup.LayoutParams(size, size));使用button.setLayoutParams(new LinearLayout.LayoutParams(size, size));

UPDATE 變化(size, size)(SIZE, SIZE)如果這是你正在使用的變量。否則,將該變量替換爲您想要的任何大小。

UPDATE 2

爲了得到屏幕的寬度和除以3,可以做到這一點: 顯示顯示= getWindowManager()getDefaultDisplay(); 點大小=新點(); display.getSize(size); INT buttonwidth =(int)的(size.x/3);

然後你簡單地傳遞變量而非SIZE像這樣:

button.setLayoutParams(new LinearLayout.LayoutParams(buttonwidth, buttonwidth)); 
+0

這是行不通的。雖然現在我的應用程序不會崩潰,但按鈕的寬度和高度並不相同。 – lmiguelvargasf

+0

我更新了我的答案。 –

+0

這是工作。但是,最終我想要的是讀取屏幕寬度,並將第三部分作爲我的按鈕寬度,但是謝謝。你的回答解決了我的問題。 – lmiguelvargasf

相關問題