我和我在幾種佈局中使用的菜單有同樣的問題。我通過在擴展了RelativeLayout的類中擴展布局xml文件來解決問題,然後在那裏定義了onClickListener。之後,我將課程包含在每個需要菜單的佈局中。該代碼是這樣的:
menu.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>
MenuView.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 />
標籤,你現在可以在其他佈局重用你selfwritten佈局(包括onClickListener)。
謝謝比利天才:)它的工作原理! – 2011-01-10 06:39:40
非常感謝。這個對我有用!!! – AD14 2013-06-11 16:39:18
不錯的解決方案,但有點長。如果您正在尋找一些簡單的解決方案,請參閱http://stackoverflow.com/a/16870468/1055241 – gprathour 2014-06-26 09:00:03