Java 在android运行时更改GridLayout行/列计数
我是android开发的新手。我正在尝试使用GridLayout来完成我的UI设计 这是一个场景: 我在xml文件中定义了GridLayout,如下所示Java 在android运行时更改GridLayout行/列计数,java,android,user-interface,android-gridlayout,Java,Android,User Interface,Android Gridlayout,我是android开发的新手。我正在尝试使用GridLayout来完成我的UI设计 这是一个场景: 我在xml文件中定义了GridLayout,如下所示 <GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/linearLayoutBo
<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>
到目前为止一切顺利。
不过,我也有一些按钮。在button的click事件中,我更改了rowNumber和colNumber,并在运行时设置它们。我想这会导致一些错误
所以我的问题是是否可以在运行时为gridLayout设置行号和列号
如果android中不允许这样做,那么实现如上所述的GUI的良好实践是什么
任何帮助都将不胜感激。今天,我想出了解决这个问题的办法 在xml文件中,我将GridLayout替换为LinearLayout,并将girdlayout动态添加为LinearLayout的子项。现在,可以在按钮单击事件处理程序中更改行/列计数
我不确定这是否是一个好方法,并且会出现任何性能问题。您需要执行gridLayout.removeAllChildren() Invalidate是一种让视图系统知道视图内容已更改并且需要重新绘制的方法
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
gridLayout.invalidate();
int rowNumber = 4;
int colNumber = 4;
gridLayout.setRowCount(rowNumber);
gridLayout.setColumnCount(colNumber);