如何设置具有不同列大小的 Android GridView

2024-01-04

我的主布局中有一个 GridView,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:horizontalSpacing="1dp"
        android:stretchMode="columnWidth"
        android:verticalSpacing="1dp" />

</LinearLayout>

which produces a grid like this enter image description here

每个单元格的内容是这样的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@drawable/btn_measurement" >

    <Button
        android:id="@+id/numerical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="---" />

</LinearLayout>

但我希望第四列是其他列的三分之一,但我做不到。

我检查了链接:

具有不同列大小的 GridView https://stackoverflow.com/questions/6651175/gridview-with-different-column-sizes?answertab=active#tab-top and Android GridView 列拉伸 https://stackoverflow.com/questions/8873488/android-gridview-column-stretching

但他们没有帮助。 :(

如果您认为我需要完全改变我的解决方案,请比仅仅提供一般线索更精确。 谢谢


将 RecyclerView(而不是 GridView)与自定义 GridLayoutManager 结合使用,如“可变跨度大小”下所示:http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html

基本上,您可以使每个常规列占用 3x,而较小的列占用 1x。

GridLayoutManager manager = new GridLayoutManager(this, 10); // 3 cols of 3 + 1 col of 1
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
  @Override
  public int getSpanSize(int position) {
    if (position % 4 == 3)
       return 1; // Fourth column is 1x
    else
       return 3; // Remaining columns are 3x
  }
});
recyclerView.setLayoutManager(manager);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何设置具有不同列大小的 Android GridView 的相关文章

随机推荐