如何在xml中使用android ExpandableListView?

如何在xml中使用android ExpandableListView?,android,expandablelistview,android-xml,Android,Expandablelistview,Android Xml,我试图在xml中显示一个可扩展的ListView,它前面有一个按钮。但不知怎的,我无法让它在代码中工作 我的xml(我使用RelativeLayout作为父布局): 我的日志: java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list' 我试图将id更改为id=“android:id/list”,但我不知道如何使用该id创建Ex

我试图在xml中显示一个可扩展的ListView,它前面有一个按钮。但不知怎的,我无法让它在代码中工作

我的xml(我使用RelativeLayout作为父布局):

我的日志:

java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'
我试图将id更改为id=“android:id/list”,但我不知道如何使用该id创建ExpandableListView对象,因为它会给出一个错误,表示其为空

*impt:我想创建一个ExpandableListView对象,以便可以强制转换onChildClickListener。

试试下面的代码-

Main.xml-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ExpandableListView android:id="@+id/listView"
    android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="none"></ExpandableListView>
</LinearLayout>

ChildLayout.xml-

<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="45dip"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/ImageView01" android:src="@drawable/icon"
    android:layout_height="40dip" android:layout_width="40dip" android:layout_marginLeft="40dip"></ImageView>
<TextView android:layout_width="fill_parent"
    android:layout_height="45dip" android:paddingLeft="5dip"
    android:paddingRight="5dip" android:textStyle="bold" android:textSize="17dip"
    android:gravity="center_vertical" android:id="@+id/tvChild"
    android:text="Children" android:textColor="#ffCCCC22"></TextView>
</LinearLayout>

Grouplayout.xml-

<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="45dip"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/title">
<ImageView android:id="@+id/ImageView01" android:src="@drawable/icon"
    android:layout_height="40dip" android:layout_width="40dip" android:layout_marginLeft="40dip"></ImageView>
<TextView android:id="@+id/tvGroup" android:layout_width="fill_parent"
    android:layout_height="45dip" android:text="Groups" android:gravity="center_vertical|right"
    android:paddingLeft="5dip" android:paddingRight="5dip"
    android:textColor="#ffffffff" android:textStyle="bold"
    android:textSize="17dip"></TextView>
</LinearLayout>

这些是可自定义扩展ListView的布局


您可以在xml文件的

中尝试使用整个代码将id(即默认id)更改为自定义id

<ExpandableListView 
    android:id="@+id/expandablelist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/mlt_file_frameL"
    >

</ExpandableListView>
对组和子组使用不同的布局

了解更多说明
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="45dip"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/title">
<ImageView android:id="@+id/ImageView01" android:src="@drawable/icon"
    android:layout_height="40dip" android:layout_width="40dip" android:layout_marginLeft="40dip"></ImageView>
<TextView android:id="@+id/tvGroup" android:layout_width="fill_parent"
    android:layout_height="45dip" android:text="Groups" android:gravity="center_vertical|right"
    android:paddingLeft="5dip" android:paddingRight="5dip"
    android:textColor="#ffffffff" android:textStyle="bold"
    android:textSize="17dip"></TextView>
</LinearLayout>
<ExpandableListView 
    android:id="@+id/expandablelist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/mlt_file_frameL"
    >

</ExpandableListView>
expLV= new ExpandableAdapter(Expandablelist.this);
setListAdapter(expLV);