为什么我包含的布局的标签返回null? [英] Why is my included layout's tag returning null?
问题描述
使用以下布局,getView(R.id.included).getView(R.id.text_view)
得出null
.如果我将TextView
包围在LinearLayout
中,问题将消失.这是怎么回事?
Using the below layouts, getView(R.id.included).getView(R.id.text_view)
evaluates to null
. If I surround the TextView
in a LinearLayout
the problem disappears. What's going on here?
layout.xml
layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include android:id="@+id/included"
layout="@layout/included" />
</LinearLayout>
included.xml
included.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/text_view"/>
推荐答案
<include>
并非完全有据可查.
Tor Norbye 写:
Tor Norbye wrote:
<include>
标记不是真实视图,因此findByView
将找不到它. @id
属性(以及您在include标记上设置的任何其他属性)将改为应用到包含布局的根标记上.因此,您的activity.getView(R.id.included1)
实际上应该是<TextView>
本身.
The <include>
tag is not a real view, so findByView
will not find it. The @id
attribute (and any other attributes you've set on the include tag) gets applied on the root tag of the included layout instead. So your activity.getView(R.id.included1)
should in fact be the <TextView>
itself.
这篇关于为什么我包含的布局的标签返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!