微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android Studio在TableLayout中平均缩放ImageButtons以适合屏幕

如何解决Android Studio在TableLayout中平均缩放ImageButtons以适合屏幕

我刚开始在第一艘项目战列舰上工作。

我正在尝试在要玩的游戏场上生成ImageButtons,但是当生成它们时,它们的大小应相同,纵横比为1:1

游戏场已生成到buttonContainer

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:background="#B3CEDD"
    android:orientation="horizontal"
    tools:context="lotistest.setShips">

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_weight="0.55">
    <TableLayout
        android:id="@+id/buttonContainer"
        android:shrinkColumns="true"
        android:stretchColumns="*"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"/>
</LinearLayout>
<LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.45">

        <RadioGroup
            android:id="@+id/shipContainer"

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"></RadioGroup>
    </LinearLayout>

   
</LinearLayout>

要在Java中生成代码

private void createBoard(Point[][] board,TableLayout parent) {
        for (int row = 0; row < board.length; row++) {
            TableRow addRow = new TableRow(this);
            ViewGroup.LayoutParams params = new TableRow.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT,0.1f);
            for (int col = 0; col < board[0].length; coL++) {
                fieldButtons[row][col] = new ImageButton(this);
                fieldButtons[row][col].setId(View.generateViewId());
                fieldButtons[row][col].setTag(new Point(row,col));
                fieldButtons[row][col].setBackgroundColor(getColor(R.color.black));
                fieldButtons[row][col].setLayoutParams(params);
                fieldButtons[row][col].setScaleType(ImageView.ScaleType.FIT_CENTER);
                fieldButtons[row][col].setimageResource(R.drawable.ic_fieldbutton);
                fieldButtons[row][col].setonClickListener(btnListener);
                addRow.addView(fieldButtons[row][col]);
            }
            addRow.setLayoutParams(params);
            parent.addView(addRow);
        }
    }

(屏幕截图中的按钮与问题无关)

Screenshot from battleships with problem

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。