更改网格布局行/列在Android的运行时数 [英] Changing GridLayout row/column count at runtime in android
问题描述
我在android开发新的。我试图用一个网格布局,以实现自己的UI设计。
I am new in android development. I am trying to use a GridLayout to fulfill my UI design.
这是该方案:
我的XML文件中定义的网格布局如下
I defined a GridLayout in xml file as following
<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>
然后,我把它在我的活动的onCreate()方法类似以下内容:
Then, I set it in my activity's onCreate() method like following:
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
gridLayout.invalidate();
int rowNumber = 4;
int colNumber = 4;
gridLayout.setRowCount(rowNumber);
gridLayout.setColumnCount(colNumber);
到目前为止好,一切运作良好。
不过,我也有一些按钮那里。在按钮的单击事件,我改变了ROWNUMBER和colNumber,设置它们在运行时。这会导致一些错误,我认为......
so far so good, everything is working well. However, I also have some buttons there. In button's click event, I changed the rowNumber and colNumber, set them at runtime. It cause some error I think...
所以我的问题是,是否行号和列号可以在运行时为网格布局进行设置。
So my question is whether the row number and column number can be set at runtime for gridLayout.
如果这不是在android系统允许的,哪些是要实现像什么上述我的GUI一个很好的做法。
If this is not allowed in android, what is a good practice to realize a GUI like what I described above.
任何帮助将是AP preciated。
Any help would be appreciated.
推荐答案
您需要做gridLayout.removeAllChildren()。
You'll need to do a gridLayout.removeAllChildren().
的Invalidate是让查看系统的方式知道查看的内容发生了变化,它需要重新绘制。
Invalidate is a way of letting the View system know that the content of the View has changed and that it needs to be redrawn.
这篇关于更改网格布局行/列在Android的运行时数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!