【问题标题】:Android Gridview without Image Adaptor没有图像适配器的 Android Gridview
【发布时间】:2012-07-13 19:13:08
【问题描述】:

我有一个空白的线性列表,我有一个将动态加载的图像列表(来自 url,工作)。

我需要在网格中显示图像(例如当您访问 facebook 照片库时)。

由于明显的原因,我不能使用 XML,我的图像显示只是它们在彼此的顶部并且非常小。

还可以轻松地将它们设置为屏幕大小的百分比吗? (所以列表在所有设备上看起来都一样)

这是目前为止的代码:

private void Display()
{
    Toast toast = Toast.makeText(getApplicationContext(), "Downloading Photos", Toast.LENGTH_SHORT);
    toast.show();

    //Get width of the image.
    int imageWidth = (int)getWindowManager().getDefaultDisplay().getWidth() / 3;
    LayoutParams gp = new GridView.LayoutParams(GridView.LayoutParams.FILL_PARENT, (int)imageWidth);

    //Placeholders
    for(int _i = 0; _i < _noOfPhotos; _i++)
    {
        ImageView imageView = new ImageView(getApplicationContext());

        imageView.setImageResource(R.drawable.no_image);
        imageView.setLayoutParams(gp);

        _imageViewArray.add(imageView);
        this.addContentView(_imageViewArray.get(_i), gp);
    }

    //Set URL

    //UrlImageViewHelper.setUrlDrawable(imageView, _userGallery.get(_i).getPicture());
}

【问题讨论】:

    标签: android facebook android-layout android-intent android-listview


    【解决方案1】:

    使用GridView,这正是这个用例。

    <GridView 
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="20dip"
        android:layout_marginBottom="20dip"
        android:numColumns="3" />
    

    你可以得到每个 ImageView 的宽度

    float width = getWindowManager().getDefaultDisplay().getWidth() / 3;
    mLayoutParams = new GridView.LayoutParams(
                GridView.LayoutParams.FILL_PARENT, 
                (int)width); // set the width as height for quadratic images
    

    然后,在 GridView 的 ListAdapter 中

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(context); // need to check convertView
        imageView.setImageResource(mImages.get(position));
        imageView.setLayoutParams(mLayoutParams);
    
        return imageView;
    }
    

    【讨论】:

    • 感谢您的回复,但我收到错误消息:错误:解析 XML 时出错:第一个 xml 位上的未绑定前缀:-/
    • 这个 xml sn-p 可以添加到例如 LinearLayout 中。如果您想单独添加 android 命名空间属性 xmlns:android="schemas.android.com/apk/res/android"
    • 它没有那么复杂。你只需要习惯ListAdapters的原理。这在 android 开发中非常常用(也适用于所有 ListViews)。这是 GridView developer.android.com/guide/topics/ui/layout/gridview.html 的一个简单示例
    • 我卡住了,你能把这个简单点吗?
    • 您必须先设置一个默认的本地图像,然后使用 AsyncTask 在后台加载远程图像。谷歌,你会发现大量的例子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多