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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...