1

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

我想做的是修改gridview内的imagebuttons的大小以及它们在单元格之间的填充。

这是我的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;
}

我怎么做?

4

2 回答 2

3

您可以设置 withsetColumnWidth(int)或通过调用来设置列数,setNumColumns(int)具体取决于您正在尝试的内容。顺便说一句,您还可以通过 xml (android:columnWidthandroid:numColumns) 设置该值。

对于间距,您可以使用android:horizontalSpacingorsetHorizontalSpacing(int)android:verticalSpacingor setVerticalSpacing(int)

为了控制你需要改变你的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;
}
于 2013-04-27T12:04:05.527 回答
1

创建一个 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;
                  }
于 2013-04-27T12:22:36.860 回答