如何自动(键入时)扩展和收缩 EditTexts 的列以显示所有符号?

How to automatically (when typing) expand and shrink columns of EditTexts to show all symbols?

我有以下代码用于创建人们输入数字的 tables(类似于 excel,但更加简化)

public class Table extends Fragment {
    private TableLayout tableLayout;
    private EditText[][] editTexts;
    private int x, y;
    private int shape;

    Table(int shape) {
        this.shape = shape;
    }

    int getX() {
        return x;
    }

    void setX(int x) {
        if (x > 0) this.x = x;
    }

    int getY() {
        return y;
    }

    void setY(int y) {
        if (y > 0) this.y = y;
    }

    void refresh() {
        tableLayout.removeAllViews();
        TableRow[] tableRows = new TableRow[y];
        editTexts = new EditText[y][x];
        for (int i = 0; i < tableRows.length; i++) {
            tableRows[i] = new TableRow(getContext());
            tableLayout.addView(tableRows[i]);
            for (int j = 0; j < editTexts[i].length; j++) {
                editTexts[i][j] = new EditText(getContext());
                editTexts[i][j].setPadding(10, 0, 10, 0);
                editTexts[i][j].setBackground(getResources().getDrawable(shape, Objects.requireNonNull(getActivity()).getApplicationContext().getTheme()));
                editTexts[i][j].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
                editTexts[i][j].setMinEms(1);
                editTexts[i][j].setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
                tableRows[i].addView(editTexts[i][j]);
            }
        }
    }

    String getElement(int x, int y) {
        return editTexts[y][x].getText().toString();
    }

    void setElement(String string, int x, int y) {
        editTexts[y][x].setText(string);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_table, container, false);
        tableLayout = new TableLayout(getContext());

        FrameLayout frameLayout = view.findViewById(R.id.frame_layout);
        frameLayout.addView(tableLayout);
        refresh();
        return view;
    }
}

但我有一个问题:这个 table 不能即时自动调整其列的大小。例如, 我输入了一些数字,我得到了这个:

图 1:

然后,我点击 phone 上的后退按钮,得到这个:

图 2:

我的问题是:如何在不按任何按钮的情况下获得如图 2 中的结果,我想在输入时 调整 table 列的大小,而不是在此之后

谢谢。

你主要面临这个问题是因为布局参数设置不正确你应该尝试在这段代码中设置布局参数:

 void refresh() {
        tableLayout.removeAllViews();
        TableRow[] tableRows = new TableRow[y];
        editTexts = new EditText[y][x];
        for (int i = 0; i < tableRows.length; i++) {
            tableRows[i] = new TableRow(getContext());
            tableLayout.addView(tableRows[i]);
            for (int j = 0; j < editTexts[i].length; j++) {
                editTexts[i][j] = new EditText(getContext());
                editTexts[i][j].setPadding(10, 0, 10, 0);
                editTexts[i][j].setLayoutParams(new 
                TableRow.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
                ViewGroup.LayoutParams.WRAP_CONTENT));
                editTexts[i][j].setBackground(getResources().getDrawable(shape, Objects.requireNonNull(getActivity()).getApplicationContext().getTheme()));
                editTexts[i][j].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
                editTexts[i][j].setMinEms(1);
                editTexts[i][j].setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
                tableRows[i].addView(editTexts[i][j]);
            }
        }
    }