0

我的程序當前允許通過滑動手指打開導航欄,但不會顯示菜單按鈕,因此我可以單擊菜單按鈕將其打開。我有onPostCreate和onOptionsItem Overide函數,但我不相信他們曾經被調用過。我該如何解決這個問題。 我的程序最低API級別是8,所以我不知道這是否是一個問題。謝謝!Android導航抽屜不添加ActionBar菜單按鈕

主要活動:

public class Home_Page extends ActionBarActivity implements AdapterView.OnItemClickListener{ 

NavigationDrawer drawerLayout; 
ListView listViewLeft, listViewRight; 
String selectedMenuItem; 
MyListViewAdapter myListViewAdapter; 
int[] images = {R.drawable.menu_icon, R.drawable.menu_icon, R.drawable.menu_icon, R.drawable.menu_icon}; 
String[] listViewLeftItems = {"Home", "Choice 2", "Choice 3", "Choice 4"}; 
Intent intent; 

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home__page); 

    drawerLayout = new NavigationDrawer(this, (DrawerLayout) findViewById(R.id.drawerLayout), getSupportActionBar()); 
    drawerLayout.createDrawer(); 

    initializeVar(); 

    myListViewAdapter = new MyListViewAdapter(this, images, listViewLeftItems); 
    listViewLeft.setAdapter(myListViewAdapter); 
    listViewLeft.setOnItemClickListener(this); 

} 

public void initializeVar(){ 
    listViewLeft = (ListView) findViewById(R.id.drawerListLeft); 
    listViewRight = (ListView) findViewById(R.id.drawerListRight); 
} 

@Override 
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
    drawerLayout.closeDrawers(); 
    switch(position){ 
     case 0: 
      selectListViewItemLeft(position); 
      new Thread() { 
       public void run() { 
        try { 
         intent = new Intent(Home_Page.this, Home_Page.class); 
         startActivity(intent); 
         finish(); 
        }catch (Exception e){ 
         e.printStackTrace(); 
        } 
       } 
      }.start(); 
      break; 
     case 1: 
      selectListViewItemLeft(position); 
      new Thread() { 
       public void run() { 
        try { 
         Intent intent = new Intent(Home_Page.this, AddAthlete.class); 
         startActivity(intent); 
         finish(); 
        }catch (Exception e){ 
         e.printStackTrace(); 
        } 
       } 
      }.start(); 
      break; 
     case 2: 
      selectListViewItemLeft(position); 
      break; 
     case 3: 
      selectListViewItemLeft(position); 
      break; 
    } 
} 

public void selectListViewItemLeft(int position){ 
    listViewLeft.setItemChecked(position, true); 
    selectedMenuItem = listViewLeftItems[position]; 
} 
} 

抽屜式導航類:(自定義類,因此可以創建不同的活動,新的導航欄)

public class NavigationDrawer extends Activity{ 
DrawerLayout drawerLayout; 
ActionBarDrawerToggle drawerToggle; 
Activity currentActivity; 
android.support.v7.app.ActionBar actionBar; 

NavigationDrawer(Context context, DrawerLayout drawerLayout, android.support.v7.app.ActionBar actionBar) { 
    this.currentActivity = (Activity) context; 
    this.drawerLayout = drawerLayout; 
    this.actionBar = actionBar; 
} 

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
public void createDrawer() { 

    drawerToggle = new ActionBarDrawerToggle(currentActivity, drawerLayout, R.drawable.menu_icon, R.string.drawer_open, R.string.drawer_closed) { 
     @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
     @Override 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
      actionBar.setTitle("Menu"); 
     } 

     @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
     @Override 
     public void onDrawerClosed(View drawerView) { 
      super.onDrawerClosed(drawerView); 
      actionBar.setTitle(getTitle()); 
     } 
    }; 
    drawerLayout.setDrawerListener(drawerToggle); 
    actionBar.setDisplayHomeAsUpEnabled(true); 
    actionBar.setIcon(android.R.color.transparent); 
    actionBar.setHomeButtonEnabled(true); 
} 
public void closeDrawers(){ 
    drawerLayout.closeDrawers(); 
} 

// Displays toggle button to expand drawer 
@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    drawerToggle.syncState(); 
    Log.e("", "onPostCreate Run"); 
} 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if(drawerToggle.onOptionsItemSelected(item)){ 
     Log.e("", "onOptionsItemSelected Run"); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    drawerToggle.onConfigurationChanged(newConfig); 
    Log.e("", "onConfigurationChanged Run"); 
} 

} 
+0

許多問題,抽屜式導航類必須是一個片段。所有的重寫方法都必須在ActionBarActivity類中。 – zIronManBox

回答

3

在你活動的佈局文件,使用抽屜佈局作爲您的父級佈局,並將工具欄添加爲子視圖。對於前:

<?xml version="1.0" encoding="utf-8"?> 
     <android.support.v4.widget.DrawerLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/DrawerLayout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:elevation="7dp"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical> 

       <include 
        android:id="@+id/tool_bar" 
        layout="@layout/tool_bar"></include> 
       <!-- Add your Main Content Here --> 

      </LinearLayout> 

      <FrameLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_gravity="left"> 

      <!-- Add your Drawer layout content here --> 

      </FrameLayout> 


     </android.support.v4.widget.DrawerLayout> 

和工具欄的佈局,tool_bar.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar             `  xmlns:android="http://schemas.android.com/apk/res/android"` 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="@color/action_bar_color" 
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark"> 

</android.support.v7.widget.Toolbar>