改变网格视图中的单元格大小?

时间:2022-09-06 19:03:14

I have a gridview in Android which is populated by a custom view which inherits from imagebuttons.

我在Android中有一个gridview,它由一个继承自imagebuttons的自定义视图填充。

What I would like to do is modify the size of the imagebuttons inside the gridview as well as their padding between cells.

我想做的是修改gridview中imagebutton的大小以及单元格之间的填充。

Here is my getView() code. I've tried using layouts but can't because I don't have access to the parent view.

这是我的getView()代码。我尝试过使用布局,但是没有,因为我不能访问父视图。

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (image == null)
        image = BitmapFactory.decodeResource(context.getResources(), R.drawable.dollar);

    ExpenseIcon ic = new ExpenseIcon(context);
    ic.setImageBitmap(image);

    return ic;
}

How do I do that?

我该怎么做呢?

2 个解决方案

#1


3  

You can set the with with setColumnWidth(int) or set the column count by calling setNumColumns(int) depending on what you extactly trying. By the way you can also set that values by xml (android:columnWidth and android:numColumns).

您可以通过调用setNumColumns(int)来设置with setColumnWidth(int),也可以通过调用setNumColumns(int)来设置列数,这取决于您正在尝试什么。顺便说一下,您还可以通过xml (android:columnWidth和android:numColumns)设置这些值。

For the spacing you can use android:horizontalSpacing or setHorizontalSpacing(int) and android:verticalSpacing or setVerticalSpacing(int).

对于间隔,可以使用android: horizontalspaceor sethorizontalspace(int)和android: verticalspaces或setverticalspace(int)。

For controling the highe you need to change your getView function:

要控制highe,您需要更改getView函数:

public YourConstructor() {
    // inflater should be a private member variable
    inflater = (LayoutInflater)context
                   .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    // other code...
}

public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        // create new one and put it into convertView. e.g.:
        convertView = new ExpenseIcon(context);
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(HEIGHT,WIDTH);
        convertView.setLayoutParams(lp);
    }

    // set the image to the currient convertView. e.g.:
    ((ExpenseIcon)convertView).setImageBitmap(image);

    return convertView;
}

#2


1  

Create an XML and inflate.I gave sample to use in getview...Set imagesize in the layout..

创建一个XML并进行膨胀。我给了getview中的示例…在布局上要合理安排尺寸。

public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;
    if (convertView == null) {
                convertView = mInflater.inflate(R.layout.grid_item, null);
                holder = new ViewHolder();
                holder.image = (ImageView) convertView.findViewById(R.id.grid_item_image);
                convertView.setTag(holder);
    }else {
                holder = (ViewHolder) convertView.getTag();
    }
                holder.image.setImageResource(list.get(position).getIconid());

                return convertView;
}

static class ViewHolder {
                ImageView image;
                  }

#1


3  

You can set the with with setColumnWidth(int) or set the column count by calling setNumColumns(int) depending on what you extactly trying. By the way you can also set that values by xml (android:columnWidth and android:numColumns).

您可以通过调用setNumColumns(int)来设置with setColumnWidth(int),也可以通过调用setNumColumns(int)来设置列数,这取决于您正在尝试什么。顺便说一下,您还可以通过xml (android:columnWidth和android:numColumns)设置这些值。

For the spacing you can use android:horizontalSpacing or setHorizontalSpacing(int) and android:verticalSpacing or setVerticalSpacing(int).

对于间隔,可以使用android: horizontalspaceor sethorizontalspace(int)和android: verticalspaces或setverticalspace(int)。

For controling the highe you need to change your getView function:

要控制highe,您需要更改getView函数:

public YourConstructor() {
    // inflater should be a private member variable
    inflater = (LayoutInflater)context
                   .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    // other code...
}

public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        // create new one and put it into convertView. e.g.:
        convertView = new ExpenseIcon(context);
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(HEIGHT,WIDTH);
        convertView.setLayoutParams(lp);
    }

    // set the image to the currient convertView. e.g.:
    ((ExpenseIcon)convertView).setImageBitmap(image);

    return convertView;
}

#2


1  

Create an XML and inflate.I gave sample to use in getview...Set imagesize in the layout..

创建一个XML并进行膨胀。我给了getview中的示例…在布局上要合理安排尺寸。

public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;
    if (convertView == null) {
                convertView = mInflater.inflate(R.layout.grid_item, null);
                holder = new ViewHolder();
                holder.image = (ImageView) convertView.findViewById(R.id.grid_item_image);
                convertView.setTag(holder);
    }else {
                holder = (ViewHolder) convertView.getTag();
    }
                holder.image.setImageResource(list.get(position).getIconid());

                return convertView;
}

static class ViewHolder {
                ImageView image;
                  }