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

标签 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>

这是我的 Activity :

// 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]);
            }
        }
    }
}

但是,我收到以下导致我的应用程序崩溃的错误。

FATAL EXCEPTION: main Process: com.mathsistor.m.tictactoe, PID: 20927 java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

最佳答案

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

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

更新 2

要获取屏幕的宽度并将其除以 3,您可以这样做: 显示 display = getWindowManager().getDefaultDisplay(); 点大小 = new Point(); 显示.getSize(尺寸); int buttonwidth = (int) (size.x/3);

然后您只需像这样传递该变量而不是 SIZE:

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

关于java - 具有相同宽度和高度的Android按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40644923/

相关文章:

java - Cordova 上的 Android Lollipop 通知图标 setColor

java - 无法实例化服务 : ClassNotFoundException

android - 具有类似聊天行为的 Recyclerview

python - Tkinter 按钮命令不运行

java - 为什么国际化不能正常运作?日本科学基金会

Java在LinkedList类中找不到符号

java - 如何使用 JSP 使用 Spring MVC 显示图像

android - 加密您的 Firestore 数据

java - 如何检测java中JFrame内的按钮被单击

wpf - 带有可调整大小圆圈的按钮控件模板