findViewById工作不适合的包括哪些内容? [英] findViewById not working for an include?
问题描述
我有一个包括像:
<include
android:id="@+id/placeHolder"
layout="@layout/test" />
在包括布局看起来像(的test.xml):
The include layout looks like (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 =内心在运行时内ImageView的:
I can't seem to find the inner ImageView with id="inner" at runtime:
ImageView iv = (ImageView)findViewById(R.id.inner);
if (iv == null) {
Log.e(TAG, "Not found!");
}
我应该能找到它?这似乎是因为它使用的是有,正常的findViewById方法是行不通的。
Should I be able to find it? It seems like since it's using an "include", the normal findViewById method does not work.
---------- ----------------更新
---------- Update ----------------
所以,我可以找到分配给该ID的包括:
So I can find the id assigned to the include:
View view = findViewById(R.id.placeHolder); // ok
但我找不到它的任何儿童的ID,如:
but I can't find any of its children by id like:
view.findViewById(R.id.outer); // nope
view.findViewById(R.id.inner); // nope
如果我尝试直接搜索他们像原来一样的:
same as the original if I try searching for them directly like:
findViewById(R.id.outer); // nope
findViewById(R.id.inner); // nope
做的IDS只是获得在运行时扒掉元素的可能?
Do ids just get stripped off of elements at runtime maybe?
感谢
推荐答案
尝试检索&LT;有/&GT;
,然后内搜索
请确保你的根具有相同的ID所包含的XML文件中的根元素..恩
Make sure your root has the same ID as the root element in the included XML file.. ex
<include
android:id="@+id/outer"
layout="@layout/test" />
然后使用检索您的内心的内容:
Then retrieve your "inner" content using:
FrameLayout outer = (FrameLayout)findViewById(R.id.outer);
ImageView iv = (ImageView)outer.findViewById(R.id.inner);
if (iv == null) {
Log.e(TAG, "Not found!");
}
这篇关于findViewById工作不适合的包括哪些内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!