【问题标题】:How to set an Android GridView with different column sizes如何设置具有不同列大小的 Android GridView
【发布时间】:2012-10-03 10:03:18
【问题描述】:

我的主布局中有一个 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>

生成这样的网格

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

<?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>

但我希望第 4 列占其他列的三分之一,但我不能。

我检查了链接:

GridView with different column sizesAndroid GridView Column Stretching

但他们没有帮助。 :(

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

【问题讨论】:

标签: android android-layout android-gridview column-width


【解决方案1】:

使用此代码使列的大小变小,但在这里你必须应用一些逻辑。第 4 列您希望它变小,因此您必须覆盖 getView() 方法,然后使用一些变量并检查该变量是否为 3 或 7 或 11 ...然后使按钮的大小变小。我想你明白我的意思....

    int logic =3;
    public View getView(int position, View convertView, ViewGroup parent) {
          Button btn;
          LinearLayout layout;
          if (convertView == null) {

            convertView = inflate the layout and initialize convertview

          if(position % logic==0)
    converView..setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                                                                LinearLayout.LayoutParams.FILL_PARENT,
                                                              (float) 0.3));


        btn = new Button(context);
        btn.setGravity(Gravity.RIGHT);
        btn.setPadding(5, 5, 5, 5);


        layout = new LinearLayout(context);
        layout.setOrientation(0);
        layout.setPadding(5, 5, 5, 10);

        btn.setText(names[position]);

        layout.addView(btn);
    }
    else {
        layout = (LinearLayout) convertView;
        btn = (Button) layout.getChildAt(0);
        btn.setText(names[position]);

    }

    return layout;
}

【讨论】:

  • 嗯,这会减小标题 TextView 和第 4 列内的按钮的大小,但不会专门减小第 4 列的大小。这意味着只有按钮和 TextView 会缩小,而不是列。
  • 好吧,你是对的,但现在你可以通过设置 layoutparams 来实现 converView
  • converView..setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT, (float) 0.3));在这一行covertView 是空的,所以很明显它会在这里给出一个异常。该怎么办?
  • 你检查/调试它,看看..它不为空。
  • converView..setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT, (float) 0.3));在这一行,covertView 为空,所以很明显它会在这里给出一个异常。该怎么办?它正在抛出空指针异常。
【解决方案2】:

使用带有自定义 GridLayoutManager 的 RecyclerView(而不是 GridView),如“可变跨度大小”下所示: 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);

【讨论】:

    【解决方案3】:

    我曾经使用过这个解决方案

    GridView with different column sizes

    通过使用列表视图而不是网格视图,并将每个项目的 4 个按钮放在一个水平线性布局中。但是你需要额外的工作来检测按钮点击,但我认为这是可行的

    我可以找到的另一项工作是使用 3rd 方库,例如这个 https://github.com/felipecsl/AsymmetricGridView

    【讨论】:

      猜你喜欢
      • 2011-10-02
      • 2015-06-10
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多