【问题标题】:Changing image view background dynamically动态更改图像视图背景
【发布时间】:2011-10-24 05:57:50
【问题描述】:

我的布局中有一组 10 个图像视图。我也给了他们连续的 ID

android:id="@+id/pb1"

android:id="@+id/pb2"

现在我想动态改变背景。

    int totalPoi = listOfPOI.size();
    int currentPoi = (j/totalPoi)*10;
    for (i=1;i<=currentPoi;i++) {
          imageview.setBackgroundResource(R.drawable.progressgreen);
}

现在在 for 循环中,我想动态设置图像视图背景。 i,e 如果 currentpoi 值为 3,则应更改 3 个图像视图的背景。无论何时 for 循环迭代许多图像视图的背景都应该更改。希望现在问题已经清楚了。

注意:我只有 1 个图像进度绿色需要设置为 10 个图像视图

【问题讨论】:

  • 能否展示完整代码并解释清楚。你有多少 drawable 。列出可绘制对象。

标签: android imageview


【解决方案1】:

最后我是通过以下方式做到的,

我把所有的id都放在了数组中

int[] imageViews = {R.id.pb1, R.id.pb2,R.id.pb3,R.id.pb4,R.id.pb5,R.id.pb6,R.id.pb7,R.id.pb8,R.id.pb9,R.id.pb10};

现在:

int pindex = 0;

for (pindex; pindex <currentPoi; pindex++) {

    ImageView img = (ImageView) findViewById(imageViews[pindex]) ;
    img.setImageResource(R.drawable.progressgreen);
}

现在,我可以动态更改图像了。

@goto10。谢谢你的帮助。我会调试你的观点,看看我这边出了什么问题

【讨论】:

    【解决方案2】:

    创建一个 ImageView 数组:

    ImageView views[] = new ImageView[10];
    views[0] = (ImageView)findViewById(R.id.pb1);
    ...
    views[9] = (ImageView)findViewById(R.id.pb10);
    

    现在迭代循环以设置图像的背景,如下所示:

    for (i=1;i<=currentPoi;i++) 
    {
        views[i-1].setBackgroundResource(R.drawable.progressgreen);
    }
    

    【讨论】:

      【解决方案3】:

      您可以通过设置可绘制对象的名称来做到这一点,例如: img_1、img_2、img_3...

      for (i=1;i<=currentPoi;i++)
      {
      ImageView imageview=(ImageView) findViewById(getResources().getIdentifier("imgView_"+i, "id", getPackageName()));
      imageview.setImageResource(getResources().getIdentifier("img_"+i, "drawable",  getPackageName()));
      }
      

      【讨论】:

      • 这不会在多个 ImageView 上设置背景。它多次在同一个 ImageView 上设置背景。
      【解决方案4】:

      试试这个代码..... 创建图像数组..

      private Integer[] mThumbIds = { R.drawable.bg_img_1, R.drawable.bg_img_2,
              R.drawable.bg_img_3, R.drawable.bg_img_4, R.drawable.bg_img_5 };
      

      然后修改你的代码

      int totalPoi = listOfPOI.size();
      int currentPoi = (j/totalPoi)*10;
      for (i=1;i<=currentPoi;i++) {
            imageview.setBackgroundResource(mThumbIds[i]);}
      

      【讨论】:

      • 这将多次更改同一图像的背景。我认为这不是我们想要的。
      • 他想根据 currentPoi 的值改变多个 ImageView 的背景。如果是 3,那么前三个(共 10 个)图像应该改变它们的背景。您的代码有 5 个背景,如果 currentPoi 为 3,则将一张图像的背景更改 3 次。
      • 这就像我让你把房间的墙壁刷成红色、绿色、蓝色和橙色,结果你把一面墙刷成红色,然后把同一面墙刷成绿色,然后再刷成蓝色,和橙子。我最终会得到一堵橙色的墙,而不是我要求的四堵油漆墙。
      • @goto10,是的,完全正确..这就是我的问题
      • 我的回答告诉你怎么做。
      【解决方案5】:

      您可以创建一个 ImageView 数组,然后在 for 循环中更改它们。

      ImageView views[] = new ImageView[10];
      views[0] = (ImageView)findViewById(R.id.imageView0);
      ...
      views[9] = (ImageView)findViewById(R.id.imageView9);
      

      然后将你的 for 循环更改为:

      for (i=1;i<=currentPoi;i++) {
         views[currentPoi].setBackgroundResource(R.drawable.progressgreen);
      }
      

      数组从索引 0 开始,因此请确保此处没有非一错误。

      【讨论】:

        【解决方案6】:

        您需要为您的 ImageViews 提供顺序 id,例如“@+id/pb1”和“@+id/pb2”等。然后您可以像这样在循环中获取它们中的每一个:

        for (i=1;i<=currentPoi;i++) {
            // Find the image view based on it's name. We know it's pbx where 'x' is a number
            // so we concatenate "pb" with the value of i in our loop to get the name
            // of the identifier we're looking for. getResources.getIdentifier() is able to use
            // this string value to find the ID of the imageView
            int imageViewId = getResources().getIdentifier("pb" + i, "id", "com.your.package.name");
        
            // Use the ID retrieved in the previous line to look up the ImageView object
            ImageView imageView = (ImageView) findViewById(imageViewId);
        
            // Set the background for the ImageView
            imageView.setBackgroundResource(R.drawable.progressgreen);
        }
        

        将com.your.package.name 替换为您的应用程序包。

        【讨论】:

        • 你能告诉我 imageview.setBackgroundResource(R.drawable.progressgreen); 中的 imageview 是什么吗?
        • imageView 设置在上一行,根据 ID 查找 ImageView。 ID 是根据 ImageView 的名称计算得出的。我将在代码中添加一些 cmets 使其更清晰。
        • 是的,但有一个愚蠢的疑问,您所指的 pb,图像视图对象是否正确?
        • 是的。 pb1、pb2等都是ImageViews。
        • 它在 ImageView imageView = (ImageView) findViewByid(imageViewId); 行中将我抛出 null,对于 imageView 虽然在上一行中 imageViewId 不为 null
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-11
        • 2013-07-16
        • 2016-05-06
        • 2014-03-12
        • 1970-01-01
        • 1970-01-01
        • 2020-07-02
        相关资源
        最近更新 更多