【问题标题】:Android Button with same width and height?具有相同宽度和高度的Android Button?
【发布时间】:2017-03-31 09:45:15
【问题描述】:

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

这是我的 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

【问题讨论】:

    标签: java android button


    【解决方案1】:

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

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

    更新 2

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

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

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

    【讨论】:

    • 这不起作用。虽然现在我的应用没有崩溃,但是按钮的宽度和高度不一样了。
    • 这行得通。但是,最终我想要的是读取屏幕宽度并将第三部分作为我的按钮宽度,但谢谢。你的回答解决了我的问题。
    • 我再次更新了我的答案,以帮助您获取屏幕宽度并将其除以 3。
    • 非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    • 2019-03-05
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    相关资源
    最近更新 更多