android - 为什么我包含的布局标签返回 null?

标签 android android-layout include

使用以下布局,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/

相关文章:

android - 为Android实现指纹认证

android - 从我的 Activity 中启动后,如何在正常操作中打开系统摄像头?

android - 如何在扩展 float 操作按钮中居中对齐文本?

android - DialogFragment 全屏显示边距

c++ - 解决 size_t 声明中的歧义

c - 使用不同的宏多次包含一个文件

android - 有没有办法让 TextView 使用 Spinner 样式?

Java 打印带有组名称的 ArrayList 组

java - 获取Android中的屏幕高度

c++ - 包括数据类型之间的斗争