【问题标题】:Changing the size of cells in grid view?更改网格视图中单元格的大小?
【发布时间】:2013-04-27 11:29:03
【问题描述】:

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

我想要做的是修改 gridview 中图像按钮的大小以及它们在单元格之间的填充。

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

我该怎么做?

【问题讨论】:

    标签: java android xml gridview


    【解决方案1】:

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

    对于间距,您可以使用android:horizontalSpacingsetHorizontalSpacing(int)android:verticalSpacingsetVerticalSpacing(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;
    }
    

    【讨论】:

    • 高度可以在您的适配器中简单地控制。我建议在 getView(...) 函数中这样做。在那里,您可以通过覆盖 convertView 的布局参数来设置高度。
    • 我一直在尝试解决它,但没有成功,我已经用一些源代码更新了我的问题......
    • 哦,不,由于性能原因,这看起来不太好,您应该使用 convertView。我将编辑我的答案...
    • 我尝试像这样添加我自己的线性参数:LinearLayout.LayoutParams l = new LinearLayout.LayoutParams(30, 30); convertView.setLayoutParams(l);但是在函数完成执行后抛出异常
    【解决方案2】:

    创建一个 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;
                      }
    

    【讨论】:

    • 使用 ViewViewholder 总是一个好主意,但你的实现没有使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多