【问题标题】:how to display text and image in GridView如何在 GridView 中显示文本和图像
【发布时间】:2014-11-30 18:05:37
【问题描述】:

这是我必须使用的my string,它存储在一个名为 txt 的数组中

文本和链接在 gridView 单元格中显示良好: public View getView(int position, View convertView, ViewGroup parent) {

 label.setText(Html.fromHtml(txt[position]));

但问题是,我怎样才能让 image 显示?这是位于菜单文件夹中的我的 xml 文件,它将填充 gridView 的每个单独的单元格:

<ImageView
    android:id="@+id/grid_image"
    android:layout_width="50dp"
    android:layout_height="50dp">
</ImageView>
<TextView
    android:id="@+id/grid_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:textSize="9sp" >
</TextView>

【问题讨论】:

    标签: android layout


    【解决方案1】:

    您应该从 URL 下载图像,然后在 ImageView 中显示它。这是示例代码:How to load an ImageView by URL in Android?

    或者您可以使用库来下载和显示图像:https://github.com/nostra13/Android-Universal-Image-Loader

    【讨论】:

      【解决方案2】:

      我使用AQuery 从互联网上下载图片,下面是我的示例:

      public static void loadImageFromUrl(ImageView imageView, String url) {
          AQuery aq = new AQuery(imageView);
          Bitmap bitmap = aq.getCachedImage(url);
          if (bitmap == null) {
                  aq.image(url, memCache, fileCache); //memCache and fileCache are booleans.
          } else {
              imageView.setImageBitmap(bitmap);
          }
      }
      

      然后在你的 gridview 中调用:

       loadImageFromUrl(R.id.grid_image, theUrlOfImage);
      

      【讨论】:

        【解决方案3】:

        您可以使用Picasso。 代码短小精悍,可以处理缓存、内存和线程。

        如果您以与声明“标签”相同的方式声明“图像视图”,则它只是一行:

        Picasso
           .with(context)
           .load(url)
           .into(imageView);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多