Android按钮具有相同的宽度和高度?

时间:2016-11-17 00:45:20

标签: java android button

我正在制作一个井字游戏,我需要使我的按钮的宽度和高度相同。

这是我的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 个答案:

答案 0 :(得分:1)

而不是button.setLayoutParams(new ViewGroup.LayoutParams(size, size));使用button.setLayoutParams(new LinearLayout.LayoutParams(size, size));

<强>更新 如果这是您正在使用的变量,请将(size, size)更改为(SIZE, SIZE)。否则,将变量替换为您想要的任何大小。

更新2

要获取屏幕宽度并将其除以3,您可以执行以下操作:     显示display = getWindowManager()。getDefaultDisplay();     Point size = new Point();     display.getSize(大小);     int buttonwidth =(int)(size.x / 3);

然后你只需传递该变量而不是SIZE,如下所示:

button.setLayoutParams(new LinearLayout.LayoutParams(buttonwidth, buttonwidth));
相关问题