【问题标题】:How can I set attr value in ImageView from class?如何在类的 ImageView 中设置 attr 值?
【发布时间】:2017-10-13 13:33:51
【问题描述】:
<ImageView
    android:id="@+id/avatar_repo_list"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:src="?attr/avatar_repo_icon"
    tools:ignore="ContentDescription" />

像这样,但来自班级

【问题讨论】:

    标签: android xml imageview attr


    【解决方案1】:

    首先通过“ID”在你的Activity类中获取你的image-view的引用,然后像这样添加attr值:

    Android: how to get value of an attribute in code?

    【讨论】:

      【解决方案2】:

      在你activity中,使用findViewById()获取这个image view的引用,然后你就可以使用这个引用来调用方法了。然后这些方法设置image view 的属性值。这是示例性的 sn-p。

      ImageView img=(ImageView) findViewById(R.id.avatar_repo_list);
      //Suppose I need to set the Drawable programatically    
      img.setImageDrawable(R.drawable.my_image);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-20
        • 2015-12-14
        相关资源
        最近更新 更多