【问题标题】:Android - Set the space between GridLayout children programatically?Android - 以编程方式设置 GridLayout 子项之间的空间?
【发布时间】:2014-10-09 21:47:17
【问题描述】:
我有一个在 XML 中创建的 GridLayout,但子/图标是在运行时添加的。由于某种原因,单元格被挤压在一起,那么如何在运行时设置单元格之间的空间?
我几乎是在按钮的 OnLongClickListener 中使用 grid.addView(icon) 将图标添加到 GridLayout。
这是我得到的:
是的,我正在制作一个启动器。
有什么解决办法吗?
谢谢!
【问题讨论】:
标签:
android
width
grid-layout
【解决方案1】:
您可以指定 16 列网格。在第一(或最后)行是一个虚拟行(仅包括第一列的下方)。
<GridLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="16" >
<TextView
android:id="@+id/grid_id_col0"
android:text="@string/grid_col0" />
<TextView
android:id="@+id/camera"
android:layout_column="0"
android:layout_columnSpan="6"
android:layout_row="1"
android:layout_marginLeft="16dp"
android:text="camera" />
<TextView
android:id="@+id/clock"
android:layout_column="10"
android:layout_columnSpan="16"
android:layout_row="1"
android:layout_marginLeft="16dp"
android:text="clock" />
</GridLayout>
在运行时,您可以指定列的宽度。
TextView colTv = (TextView) inRootView.findViewById(R.id.grid_id_col0);
colTv.setWidth(colWidth);
colTv.setVisibility(View.INVISIBLE);
colWidth 由设备宽度除以列数得出。
如果您留下一些没有任何内容的列,则两个图像之间的空间存在。在上面的示例中,第 1 行的第 6 到 9 列没有内容。