6

I have a GridView in my main layout as follows:

<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

the contents of each cell is a layout like this:

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

But I want the 4th column to be one-third of other columns and I just can't.

I checked the links:

GridView with different column sizes and Android GridView Column Stretching

but they were of no help. :(

if you think that I need to change my solution entirely please be more precise than just giving a general clue. Thanx

4

3 回答 3

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;
}
于 2012-10-13T06:20:11.713 回答
1

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

您基本上会让每个常规列占用 3 倍,而较小的列占用 1 倍。

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);
于 2015-12-31T02:59:42.213 回答
0

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

具有不同列大小的 GridView

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

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

于 2015-12-31T02:42:35.957 回答