我在几个布局中使用的菜单也有同样的问题。我通过在扩展RelativeLayout的类中扩展布局xml文件解决了这个问题,然后我在其中定义了onClickListener。之后,我在需要菜单的每个布局中都包含了该类。代码如下所示:
菜单.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton android:id="@+id/map_view"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:src="@drawable/button_menu_map_view"
android:background="@null"
android:scaleType="fitCenter"
android:layout_height="@dimen/icon_size"
android:layout_width="@dimen/icon_size">
</ImageButton>
<ImageButton android:id="@+id/live_view"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:src="@drawable/button_menu_live_view"
android:background="@null"
android:scaleType="fitCenter"
android:layout_height="@dimen/icon_size"
android:layout_width="@dimen/icon_size">
</ImageButton>
<ImageButton android:id="@+id/screenshot"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:src="@drawable/button_menu_screenshot"
android:background="@null"
android:scaleType="fitCenter"
android:layout_height="@dimen/icon_size"
android:layout_width="@dimen/icon_size">
</ImageButton>
</merge>
菜单视图.java
public class MenuView extends RelativeLayout {
private LayoutInflater inflater;
public MenuView(Context context, AttributeSet attrs) {
super(context, attrs);
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.menu, this, true);
((ImageButton)this.findViewById(R.id.screenshot)).setOnClickListener(screenshotOnClickListener);
((ImageButton)this.findViewById(R.id.live_view)).setOnClickListener(liveViewOnClickListener);
((ImageButton)this.findViewById(R.id.map_view)).setOnClickListener(mapViewOnClickListener);
}
private OnClickListener screenshotOnClickListener = new OnClickListener() {
public void onClick(View v) {
getContext().startActivity(new Intent(getContext(), ScreenshotActivity.class));
}
};
private OnClickListener liveViewOnClickListener = new OnClickListener() {
public void onClick(View v) {
getContext().startActivity(new Intent(getContext(), LiveViewActivity.class));
}
};
private OnClickListener mapViewOnClickListener = new OnClickListener() {
public void onClick(View v) {
getContext().startActivity(new Intent(getContext(), MapViewActivity.class));
}
};
}
布局.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView android:id="@+id/surface"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="fill_parent">
</SurfaceView>
<!-- some more tags... -->
<com.example.inflating.MenuView
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
使用<com.example.inflating.MenuView />
标签,您现在可以在其他布局中重用您自己编写的布局(包括 onClickListener)。