对话行为很疲惫。同时添加gridview

时间:2015-11-21 18:13:16

标签: android android-layout gridview alertdialog

我想借助网格视图在对话框中显示图像。它工作正常,但我打开Dialog有点问题。正如你在截图中看到的那样。当我打开对话框时,它会显示但非常小。

enter image description here

但是在向下滚动到2或3个项目后,它会自动切换到更大的尺寸。我不明白为什么?

enter image description here

到目前为止我尝试了什么

显示对话框的代码

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.image_select_layout, null);
        final GridView grid = (GridView) layout
                .findViewById(R.id.select_gridview);
        final Dialog dialog = new Dialog(ArtFun.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(grid);

        grid.setAdapter(new ImagesAdapter(ArtFun.this));
        grid.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
            Toast.makeText(getApplicationContext(), "position = "+ position, 1).show();

            }
        });
        dialog.show();

image_select_layout.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/select_gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"      
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"       
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"    
    android:gravity="center" 
    android:background="@drawable/imageselect_bg"    
/>

我的ImageAdapter就是这个

public class ImagesAdapter extends BaseAdapter {

    private LayoutInflater gridInflater;
    private final int BACKGROUND_IDS[] = new int[] { R.drawable.image_004,
            R.drawable.image_005, R.drawable.img, R.drawable.image_004,
            R.drawable.image_005, R.drawable.img};
    private ImageLoader imageLoader;

    public ImagesAdapter(Context context){

        gridInflater = LayoutInflater.from(context);
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return BACKGROUND_IDS.length;
    }

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

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

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        ViewHolder holder;
        if (convertView == null) {
            convertView = gridInflater.inflate(R.layout.image_adapter_layout, null);
            holder = new ViewHolder();
            holder.img = (ImageView) convertView.findViewById(R.id.adapter_view);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        @SuppressWarnings("deprecation")
        DisplayImageOptions option = new DisplayImageOptions.Builder()
        .showImageForEmptyUri(R.drawable.image_005)
        .showImageOnFail(R.drawable.image_005).cacheOnDisc(false)
        .cacheInMemory(false).displayer(new FadeInBitmapDisplayer(500))
        .build();

        String uri = "drawable://" + BACKGROUND_IDS[position];

        imageLoader.displayImage(uri, holder.img, option);

        return convertView;
    }

     static class ViewHolder {
            ImageView img;
        }
}

Image_adapter_layout.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/adapter_view"
    android:layout_width="100dip"
    android:layout_height="100dip"
    android:contentDescription="@string/app_name"
    android:padding="0dp"
    android:scaleType="fitCenter" />

我现在想要什么

我想每次都显示对话框,如第二次截图所示。提前致谢。

2 个答案:

答案 0 :(得分:0)

您可以像这样为对话框设置自定义宽度和高度。

dialog.getWindow().setLayout(width, height);

答案 1 :(得分:0)

我设法自己解决这个问题...因为我展示的图像尺寸较大..我只是用缩略图替换它们。它确实有用..我真的不知道为什么对话框最初太小了......但是在改变图像后它对我有用。

如果有人知道原因,请分享谢谢。