我有一个类似这样的include:
<include
android:id="@+id/placeHolder"
layout="@layout/test" />
include布局如下(test.xml):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/outer"
... >
<ImageView
android:id="@+id/inner"
... />
</FrameLayout>
我似乎无法在运行时找到ID为"inner"的内部ImageView:
ImageView iv = (ImageView)findViewById(R.id.inner);
if (iv == null) {
Log.e(TAG, "Not found!");
}
我应该能找到它吗?似乎由于使用了“include”,正常的findViewById方法无法工作。
---------- 更新 ----------------
所以我可以找到分配给include的id:
View view = findViewById(R.id.placeHolder); // ok
但我无法通过id找到任何它的子元素,例如:
view.findViewById(R.id.outer); // nope
view.findViewById(R.id.inner); // nope
如果我直接搜索它们,结果会与原始结果相同:
findViewById(R.id.outer); // nope
findViewById(R.id.inner); // nope
在运行时,id 在 <a> 元素中是否被剥离了?
谢谢
findViewById()
中使用android.R.id.
而不是R.id.
。一旦我解决了这个问题,dymmeh的解决方案就很好用了。 - jhavatar