【问题标题】:How can I convert String to Drawable如何将字符串转换为可绘制
【发布时间】:2014-03-18 08:28:54
【问题描述】:

我在可绘制文件夹中有很多图标,我将它们的名称命名为字符串。如何访问可绘制文件夹并更改背景 imageView(或任何视图)动态使用这些名称。谢谢

【问题讨论】:

    标签: android android-drawable android-background


    【解决方案1】:

    这可以使用反射来完成:

    String name = "your_drawable";
    final Field field = R.drawable.getField(name);
    int id = field.getInt(null);
    Drawable drawable = getResources().getDrawable(id);
    

    或者使用Resources.getIdentifier():

    String name = "your_drawable";
    int id = getResources().getIdentifier(name, "drawable", getPackageName());
    Drawable drawable = getResources().getDrawable(id);
    

    然后在任一情况下使用它来设置可绘制对象:

    view.setBackground(drawable)
    

    【讨论】:

    • 我应该使用view.setBackground(drawable) 还是view.setBackgroundResource(id) 还是什么?
    • 非常感谢。这是工作:) 所有答案都是正确的
    • 很高兴我能帮上忙。我赞成大多数其他答案以补偿接受。
    • 谢谢 :) 由于我的声誉,我不能这样做 :)
    • 这对我不起作用,但我在这里找到了解决方案:stackoverflow.com/questions/13351003/find-drawable-by-string
    【解决方案2】:
    int resId = getResources().getIdentifier("your_drawable_name","drawable",YourActivity.this.getPackageName());
    Drawable d = YourActivity.this.getResources().getDrawable(resId);
    

    【讨论】:

      【解决方案3】:

      可以这样做:

      ImageView imageView = new ImageView(this);
      imageView.setBackground(getResources().getDrawable(getResources().getIdentifier("name","id",getPackageName())));
      

      【讨论】:

        【解决方案4】:

        试试这个:

        public Bitmap getPic (int number)
        {
            return
                BitmapFactory.decodeResource
                (
                    getResources(), getResourceID("myImage_" + number, "drawable", getApplicationContext())
                );
        }
        
        protected final static int getResourceID
        (final String resName, final String resType, final Context ctx)
        {
            final int ResourceID =
                ctx.getResources().getIdentifier(resName, resType,
                    ctx.getApplicationInfo().packageName);
            if (ResourceID == 0)
            {
                throw new IllegalArgumentException
                (
                    "No resource string found with name " + resName
                );
            }
            else
            {
                return ResourceID;
            }
        }
        

        【讨论】:

        • 谢谢。我确定这是真的,但我没有尝试过 :)
        【解决方案5】:

        如果你有文件名作为字符串,你可以使用:

        int id = getResources().getIdentifier("name_of_resource", "id", getPackageName());
        

        使用这个 id,您可以像往常一样访问它(假设它是可绘制的):

        Drawable drawable = getResources().getDrawable(id);
        

        【讨论】:

          【解决方案6】:

          用例如果没有在任何活动中,使用@FD_示例

          注意:

          如果您没有参与任何活动,则必须发送上下文参数才能使用“getResources()”或“getPackageName()”,并且不推荐使用“getDrawable(id)”,请使用 getDrawer(int id, Theme theme ) 反而。 (主题可以为空):

          String name = "your_drawable";
          int id = context.getResources().getIdentifier(name, "drawable", 
          context.getPackageName());
          Drawable drawable = context.getResources().getDrawable(id, null);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-01-16
            • 1970-01-01
            • 2014-08-05
            • 1970-01-01
            • 1970-01-01
            • 2013-06-24
            • 2016-02-10
            • 2015-08-17
            相关资源
            最近更新 更多