Android按标签查找ImageView

时间:2015-09-19 03:05:55

标签: android imageview android-imageview

我只是想通过标记找到ImageView,类似于findViewById。我找到了findViewWithTag函数,但是当我尝试使用它时,我得到了“无法解析方法...”错误。

setContentView(R.layout.activity_detailed_view);

//This works fine
TextView name =  (TextView) findViewById(R.id.name);

//this doesn't
ImageView img = (ImageView) findViewWithTag("myTag");

1 个答案:

答案 0 :(得分:2)

ActivityView都有findViewById()Activity没有findViewWithTag()。试试这个:

ImageView img = (ImageView) findViewById(R.id.activity_detailed_view).findViewWithTag("myTag");

为此,您需要进入您的activity_detailed_view.xml布局,并为第一个容器指定一个“@ + id / activity_detailed_view”的ID。

换句话说,使用findViewById获取活动的视图,然后在该视图上调用findViewWithTag()