如何在课堂上的图像视图中设置attr值


<ImageView
    android:id="@+id/avatar_repo_list"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:src="?attr/avatar_repo_icon"
    tools:ignore="ContentDescription" />

像这样,但来自班级

首先通过"ID"获取活动类中图像视图的引用,然后添加如下属性值:

安卓:如何在代码中获取属性的值?

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

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

最新更新