我只是想通过标记找到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");
答案 0 :(得分:2)
Activity
和View
都有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()
。