有两种方法可以实现这个。
第一种是通过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>
而child_layout1
和child_layout2
只是另外两个布局,第二个布局包含一个带有id为mbutton
的Button
。
您的应用程序入口应该像这样:
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
和一个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
和一个android.widget.Button
。
任何一种解决方案都应该可以正常工作。
更新
你的expandableListView
是问题所在,因为 childLayout 直到您打开/展开部分时才被填充(查看下面的代码片段以检查当前视图树)。
其中一种解决方案可能是初始化checkedChangeListener
,将监听器添加到 expandable listview 中,当展开可扩展列表视图的子项时,检查您的 Checkbox
是否是已填充的子项之一,如果是,则添加checkedchangeListener
。
可能更直接的方法是:
<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;
}
some_layout
,然后再从该对象中搜索复选框吗? - Joel Bodega