如何设置一个Android GridView的不同列大小如何设置、大小、不同、GridView

2023-09-08 09:11:44 作者:一无所有就是打拼的理由

我在我的主要布局在GridView如下:

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

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

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>

但我想第4列是三分之一等栏目,我只是不能。

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

我查了一下链接:

GridView不同的列大小和的Andr​​oid GridView的列拉伸

但他们没有帮助。 :(

but they were of no help. :(

如果你认为我需要改变我的解决方案完全请更precise不仅仅是给人一种通用的线索。 感谢名单

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

推荐答案

使用code键使列小的尺寸,但在这里我只好涂抹一些逻辑。你希望它是小了4个列,因此u必须重写getView()方法,然后给出使用某个变量,并检查,如果该变量为3或7或11 ......然后做出按钮的大小小。我觉得你得到了我的观点......

use this code to make size of column small but here u have to apply some logic . the 4 th column you want it to be small so u have to override the getView() method and then give use some variable and check that if that variable is 3 or 7 or 11... then make the size of button small . i think u got my point....

    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;
}