【问题标题】:Why setImageResource displays nothing?为什么 setImageResource 什么都不显示?
【发布时间】:2011-03-26 16:37:01
【问题描述】:

我想根据数据库值在我的 ListView 中显示一个图标。我按照this 回答这样做。 但结果什么都没有显示。这是我的 row.xml 中的内容:

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

    <ImageView
        android:id="@+id/movie_subscribed_icon"
        android:padding="3dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/star_off"/>

    <LinearLayout 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical">

     <TextView android:id="@+id/movie_name"
     ...

这里是代码:

    movies.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
          int viewId = view.getId();
          switch(viewId) {
          case R.id.movie_name:
                  int readValue = cursor.getInt(cursor.getColumnIndexOrThrow(MoviesDbAdapter.KEY_READ));
                  if (readValue == 1) { // viewed movie item
                      TextView movieName = (TextView) view;
                      movieName.setTextColor(Color.GRAY);
                  }
                  break;
          case R.id.movie_subscribed_icon:
              int subscribedValue = cursor.getInt(cursor.getColumnIndexOrThrow(MoviesDbAdapter.KEY_READ));
                  if (subscribedValue > 0) { // subscribed movie item
                      ImageView movieIcon = (ImageView) view;
                      movieIcon.setImageResource(R.drawable.star_off);
                  }
              break;
          }
          return false;
        }

      } );

我特别在我的代码中使用与默认图标相同的图标。 这里有什么问题? (我只有在drawable-hdpidrawable-mdpi 文件夹中有star_off

更新。以下代码运行良好:

movieIcon.setImageDrawable(getResources().getDrawable(R.drawable.star_off));

【问题讨论】:

  • 顺便说一句,我在 LogCat 中看到以下内容:WARN/ImageView(7293): Unable to find resource: 2 WARN/ImageView(7293): android.content.res.Resources$NotFoundException: Resource ID #0x2
  • 即使我将R.drawable.star_off 更改为android.R.drawable.star_off 我也有这个问题和这样的警告。

标签: android resources android-listview android-imageview


【解决方案1】:

public void setImageResource (int resId) :这会在 UI 线程上进行位图读取和解码,这可能会导致延迟打嗝。如果这是一个问题,请考虑使用 setImageDrawable(Drawable) 或 setImageBitmap(Bitmap) 和 BitmapFactory。

尝试使您的图像视图无效,或使用文档建议的替代方法之一。

【讨论】:

  • 您能否澄清一下“无效”是什么意思?
  • "绘制是通过遍历树并渲染与无效区域相交的每个视图来处理的。因为树是按顺序遍历的,这意味着父母将在他们的孩子之前(即之后)绘制, 兄弟姐妹按照它们在树中出现的顺序绘制。如果您为视图设置背景可绘制对象,则视图将在回调其 onDraw() 方法之前为您绘制它。请注意,框架不会绘制视图不在无效区域中。要强制绘制视图,请调用 invalidate()。"
  • 嗯。奇怪的事情发生了。它开始与setImageDrawable 一起工作,但现在它不再工作了。如果我在setImageDrawable 之后立即致电movieIcon.invalidate(); - 它没有帮助。
【解决方案2】:

我在另一个问题中找到了解决方案。

基本上是 setViewValue() 方法 应该返回 false 直到它被调用 为您的图像视图。那么应该 在视图中设置数据并返回 真的。返回值表示 ViewBinder 是否设置视图 本身或适配器是否应该 通过默认值绑定数据本身 行为。

由于我没有返回 true,所以它工作不正确。 现在它完美地工作了。

【讨论】:

    【解决方案3】:

    尝试 #2。 你为什么不试试这个……

    ImageView movieIcon = (ImageView)findViewById(R.id.movie_subscribed_icon);
    movieIcon.setImageResource(R.drawable.star_off);
    

    我认为您可能正在创建一个新的 ImageView 而不是抓取布局中已经存在的那个。

    【讨论】:

      【解决方案4】:

      这段代码解决了问题:

      ImageView movieIcon = (ImageView)findViewById(R.id.movie_subscribed_icon);
      Drawable drawable = this.getResources().getDrawable(R.drawable.android);
      movie_subscribed_icon.setBackgroundDrawable(drawable);
      

      【讨论】:

        【解决方案5】:

        这行得通...

        在 XML 中,不要对 ImageView drawable 使用“src”属性。而是使用“背景”。

        像这样:

        <ImageView
                android:id="@+id/maximize_inbox_window"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_action_original_image"
                android:layout_gravity="center_vertical"
                android:clickable="true"
                android:paddingRight="5dp" />
        

        在运行时更改图像的 Java 代码在这里...

        ImageView img = (ImageView) rootView.findViewById(R.id.image_view_name);
        img.setBackground(getResources().getDrawable(R.drawable.resource_id));
        img.invalidate();
        

        将 image_view_name 替换为 XML 中的 ImageView,将 resource_id 替换为可绘制文件夹中的图像名称。不要忘记调用 invalidate()。

        注意:不要使用 setBackgroundDrawable() 作为它的弃用。

        希望这也适用于其他人。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多