使用以下布局,getView(R.id.included).getView(R.id.text_view)
的计算结果为 null
。如果我将 TextView
包围在 LinearLayout
中,问题就会消失。这是怎么回事?
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"/>
最佳答案
<include>
不完全是 well documented .
Tor Norbye wrote :
<include>
标签不是真实 View ,所以 findByView
不会找到它。 @id
属性(以及您在 include 标签上设置的任何其他属性)改为应用于包含布局的根标签。所以你的 activity.getView(R.id.included1)
实际上应该是 <TextView>
本身。
关于android - 为什么我包含的布局标签返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12221865/