Why is my included layout's tag returning null

2019-07-19 05:44发布

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

<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

<?xml version="1.0" encoding="utf-8"?>
<TextView
   android:id="@+id/text_view"/>

1条回答
forever°为你锁心
2楼-- · 2019-07-19 06:08

<include> isn't exactly well documented.

Tor Norbye wrote:

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.

查看更多
登录 后发表回答