0
我是機器人新手請幫助我。我創建了一個包含我想要在所有活動中使用它的操作欄代碼的類。我如何在我的活動中運行/調用它? 謝謝。android自定義actionbar使用?
動作條類:
public class CustomActionBar extends Activity {
public void actionbarMethod() {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View actionBarView = inflater.inflate(R.layout.custom_actionbar, null);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(actionBarView, new ActionBar.LayoutParams
(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
final DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ImageView btnmenu = (ImageView) findViewById(R.id.btnmenu);
btnmenu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
} else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
}
});
}
}
**動作條XML佈局:**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#36abcb" >
<ImageView
android:id="@+id/btnmenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:padding="8dip"
android:src="@drawable/ic_drawer"
android:background="#36abcb"/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="8dp"
android:layout_toLeftOf="@+id/btnmenu"
android:textSize="18dip" />
</RelativeLayout>
坦。它工作正常,但當我點擊主要活動中的菜單按鈕時,它的工作原理,但在其他活動中它崩潰。我該如何解決它? –
您需要將主要活動的菜單功能移至CustomActionBar。 –
我定義的菜單按鈕,然後在CustomActionBar活動的點擊監聽器,但它仍然崩潰 –