如何以编程方式在网格视图中对齐图像?

时间:2016-05-25 12:26:58

标签: android android-alertdialog android-gridview imagelist

我正在显示一个对话框来显示图像列表并选择其中一个。我已经创建了一个适配器来在网格视图中设置图像。

我已关注此链接:http://www.tutorialspoint.com/android/android_grid_view.htm,以了解相关信息。

但是现在对话框显示彼此相距很远的图像并且没有正确对齐。

Dialog现在如下:

enter image description here

适配器:

 public class ImageAdapter extends BaseAdapter {
    Context Context;
    int [] imageId;

    public ImageAdapter(Context context, int[] prgmImages) {
        // TODO Auto-generated constructor stub
        Context = context;
        imageId = prgmImages;

    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(Context);
            imageView.setLayoutParams(new GridView.LayoutParams(85,85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(2,2,2,2);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(imageId[position]);
        return imageView;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return imageId.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

}

显示对话框代码:

 private void showAlertDialog() {

    GridView gridView = new GridView(this);

    int[] mThumbIds = {
            R.drawable.roundicons05,R.drawable.roundicons08,
            R.drawable.roundicons16,R.drawable.roundicons37,
    };

    gridView.setAdapter(new ImageAdapter(CheckListActivity.this,mThumbIds));
    gridView.setNumColumns(3);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // do something here
        }
    });

    // Set grid view to alertDialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(gridView);
    builder.setTitle("Select icon");
    builder.show();

}

如何正确对齐此示例,例如在中央,4行图像等等。谢谢。

4 个答案:

答案 0 :(得分:1)

要显示正确的ImageView,请制作xml布局文件并应用适当的填充和边距 在适配器的getView方法中膨胀这个xml 像这样:

您的xml布局grid_item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical">

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

适配器的getView方法:

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    ImageView imageView;

    if (convertView == null)
    {
        LayoutInflater infalInflater = (LayoutInflater)    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.grid_item, null);
      imageView = (ImageView) convertView.findViewById(R.id.imgDoc);
        convertView.setTag(viewHolder);
    }
    else
    {
       imageView = (ImageView) convertView;
    }

 imageView.setImageResource(imageId[position]);      
 return convertView;
    }

更正确的布局在Gridview中应用horizo​​ntalSpacing和verticalSpacing。

    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"

答案 1 :(得分:0)

尝试

imageView.setLayoutParams(new GridView.LayoutParams(48,48));

答案 2 :(得分:0)

我建议你试试setAdjustViewBounds为真。它将对齐imageview

答案 3 :(得分:0)

// Add extern declarations

int main(int argc, char *argv[]) {
    void *v = make_vec(); // Use a real typedef here
    add_number(v, 42);
    print_vec(v);
    drop_vec(v);
}

要动态设置这些属性,因为你说你正在动态添加网格视图,那么这就是你可以通过java设置这些属性的方法

<GridView

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="150dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:listSelector="#fff"
    android:numColumns="3"
    android:scrollbars="none"
    android:stretchMode="spacingWidthUniform"
    android:paddingTop="10dp"
    android:layout_margin="5dp">

</GridView>