有两种方法可以实现这一点。
第一个是通过 XML 添加子布局:
1.XML方法
my_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/child_layout1" >
</include>
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/child_layout2" />
</LinearLayout>
And child_layout1
and child_layout2
只是另外两个布局,第二个有一个Button
带身份证mbutton
.
您的应用程序条目应如下所示:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
View v = findViewById(R.id.mbutton);
System.out.println(v == null);
System.out.println(v.getClass().getName());
}
}
这让我false
and a android.widget.Button
,正如我所怀疑的那样。
2. 程序化方法
另一种方法是编程方法,既然你这样描述它,我怀疑这就是你所做的(或至少尝试过的):
noinclude_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/inclusionlayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
</LinearLayout>
还有一个稍大的应用程序入口点:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.noinclude_layout);
ViewGroup inclusionViewGroup = (ViewGroup)findViewById(R.id.inclusionlayout);
View child1 = LayoutInflater.from(this).inflate(
R.layout.child_layout1, null);
View child2 = LayoutInflater.from(this).inflate(
R.layout.child_layout2, null);
inclusionViewGroup.addView(child1);
inclusionViewGroup.addView(child2);
View v = findViewById(R.id.mbutton);
System.out.println(v == null);
System.out.println(v.getClass().getName());
}
}
这也让我false
and a android.widget.Button
.
任何一种解决方案都应该可以正常工作。
UPDATE
Your expandableListView
是问题所在,因为在您实际打开/展开该部分之前,childLayout 不会膨胀(请参阅下面的代码,了解检查当前视图树的一小段代码)。
一种解决方案可能是初始化checkedChangeListener
,向可扩展列表视图添加一个侦听器,然后当打开可扩展列表视图的子视图时,检查您的Checkbox
是膨胀的孩子之一,并添加checkedchangeListener
if so.
一种可能更直接的方法是:
<CheckBox
...
android:onClick="checkclicked" />
并在你的Activity
,添加方法public void checkclicked(View view){ ... }
ViewTree打印机
private void printFullTree() {
printTree(this.getWindow().getDecorView(),0);
}
private void printTree(View view, int indent) {
System.out.print(indent(indent) + view.getClass().getName() + "; " + view.getId());
if (view instanceof ViewGroup) {
ViewGroup vg = (ViewGroup)view;
System.out.print("; children = " + vg.getChildCount() + "\n");
for (int i = 0; i< vg.getChildCount(); i++) {
printTree(vg.getChildAt(i), indent++);
}
}
else
System.out.print("\n");
}
private String indent(int indent) {
String result = "";
for (int i = 0; i < indent; i++) {
result += " ";
}
return result;
}