gpt4 book ai didi

java - 在运行时在 android 中更改 GridLayout 行/列数

转载 作者:太空狗 更新时间:2023-10-29 14:22:59 25 4
gpt4 key购买 nike

我是 android 开发的新手。我正在尝试使用 GridLayout 来完成我的 UI 设计。

这是场景:

我在 xml 文件中定义了一个 GridLayout,如下所示

<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/linearLayoutBottomLeft"
android:layout_alignParentLeft="true"
android:layout_below="@id/linearLayoutTopLeft"
android:layout_toLeftOf="@id/textView" >
</GridLayout>

然后,我在 Activity 的 onCreate() 方法中设置它,如下所示:

GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
gridLayout.invalidate();
int rowNumber = 4;
int colNumber = 4;
gridLayout.setRowCount(rowNumber);
gridLayout.setColumnCount(colNumber);

到目前为止一切顺利,一切正常。但是,我那里也有一些按钮。在按钮的点击事件中,我更改了rowNumber 和colNumber,在运行时设置它们。我认为它会导致一些错误...

所以我的问题是gridLayout是否可以在运行时设置行号和列号。

如果这在 android 中是不允许的,那么像我上面描述的那样实现 GUI 的好习惯是什么。

如有任何帮助,我们将不胜感激。

最佳答案

您需要执行 gridLayout.removeAllChildren()。

Invalidate 是一种让 View 系统知道 View 的内容已经改变并且需要重新绘制的方法。

关于java - 在运行时在 android 中更改 GridLayout 行/列数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15073941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com