【问题标题】:Center gidlayout中心布局
【发布时间】:2018-03-28 09:31:24
【问题描述】:
您好,我创建了一个包含 4 列的网格布局
但我有 7 个底部的 3 个我想居中
我想让我的网格布局像这样。
我怎样才能实现它?
我的网格布局是这样的
<GridLayout
android:layout_width="242dp"
android:layout_height="fill_parent"
android:id="@+id/gridLayout"
android:columnCount="4"
android:layout_gravity="center"
android:stretchMode="columnWidth">
// 附加
for(int x = 0; x < something.length(); x++) {
View view = to do///
gridLayout.addView(view);
}
【问题讨论】:
标签:
android
android-gridlayout
【解决方案1】:
如果您将 Recyclerview 与 GridLayout 一起使用,那么有一个选项可以实现这一目标:
<android.support.v7.widget.RecyclerView
android:layout_gravity="center"/>
并且不要忘记在 rawfile 主布局中提供 Padding ,否则所有单元格都进入中心但它们之间没有空间。
【解决方案2】:
感谢https://stackoverflow.com/a/34735650/3481654,我找到了解决方案
我改用嵌套线性布局
int cols = 4; // no of columns display per row
Linear llItems = // linear layout container
LinearLayout layout = createLinearLayout();
llItems.addView(layout);
for(int x = 0; x < bonusInfo.length(); x++) {
// create linear layout container to align
if (x != 0 && x % cols == 0) {
layout = createLinearLayout();
llItems.addView(layout);
}
if (layout != null) {
//insert items in created rows
View item = inflater.inflate(layoutId, null);
layout.addView(item);
}
}