public View inflate(int Resourece,ViewGroup root)
reSource:View的layout的ID
root: 生成的层次结构的根视图
return 填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。mail.xml文件里的层次结构是这样的
<LinearLayout android:id="@+id/LinearLayout01" />
<TextView android:id="@+id/tv1" />
<LinearLayout android:id="@+id/LinearLayout02" />
<TextView android:id="@+id/tv2" />如果我使用函数
View view = inflate.inflate(R.layout.main,(ViewGroup)findViewById(R.id.LinearLayout02));
这样是不是LinearLayout02就对应成了根视图,使用view.findViewById就找不到id为tv1的TextView了呢。我试了一下其实不是这样的,还是可以找到的。inflate这个函数的第二个参数设置到底有什么用呢
reSource:View的layout的ID
root: 生成的层次结构的根视图
return 填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。mail.xml文件里的层次结构是这样的
<LinearLayout android:id="@+id/LinearLayout01" />
<TextView android:id="@+id/tv1" />
<LinearLayout android:id="@+id/LinearLayout02" />
<TextView android:id="@+id/tv2" />如果我使用函数
View view = inflate.inflate(R.layout.main,(ViewGroup)findViewById(R.id.LinearLayout02));
这样是不是LinearLayout02就对应成了根视图,使用view.findViewById就找不到id为tv1的TextView了呢。我试了一下其实不是这样的,还是可以找到的。inflate这个函数的第二个参数设置到底有什么用呢
View view = inflate.inflate(R.layout.main,null)
setContentView(View);
View new= inflate.inflate(R.layout.main,(ViewGroup)findViewById(R.id.LinearLayout02))这样才能不为null.
哦,对的,
View new= inflate.inflate(R.layout.main,(ViewGroup)findViewById(R.id.LinearLayout02))
但是我后来程序又写tx1 = (TextView)view2.findViewById(R.id.tv1)
还是可以找到,可是tx1是不在LinearLayout02下面的是在LinearLayout01下面的
<LinearLayout android:id="@+id/LinearLayout01" />
<TextView android:id="@+id/tv1" />
<LinearLayout android:id="@+id/LinearLayout02" />
<TextView android:id="@+id/tv2" />你几个布局都是并列的关系,没有父子层级关系,你把tv1放到LinearLayout01里面再试试看能不能获取到。