2016-11-18 94 views
-1

我研究了在Android上設置導航菜單。我已經找到了一些關於如何設置它的答案,但是我的問題是它甚至不可見,如果這個問題很直接,但是在這個編程語言中真的很新,那麼很抱歉。Android導航菜單設置

這裏是我的代碼:

activity_main.xml文件:

enter code here 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.example.mywindows.neo_datawaysconsultantsandtrainers.MainActivity"> 


    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:elevation="4dp" 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
     android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

    <android.support.v4.widget.DrawerLayout 
     android:id="@+id/drawer_layout" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent"> 





     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Hello World!" /> 

     <ListView 
      android:id="@+id/drawer" 
      android:layout_width="240dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:background="@android:color/white" /> 

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


</LinearLayout> 

strings.xml文件:

<resources> 
    <string name="app_name">NEO-DATAWAYS</string> 
    <string name="drawer_opened">Select an item</string> 
    <string name="drawer_closed">NEODATAWAYS</string> 
</resources> 

styles.xml文件:

enter code here 

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">#006400</item> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="android:textColorPrimary">#FFFFFF</item> 
     <item name="android:textColorSecondary">#FFFFFF</item> 
    </style> 

</resources> 

activity_main.java文件:

enter code here 

package com.example.mywindows.neo_datawaysconsultantsandtrainers; 

import android.content.res.Configuration; 
import android.os.PersistableBundle; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.view.MenuItem; 
import android.view.View; 

public class MainActivity extends AppCompatActivity { 


    private DrawerLayout mDrawLayout; 
    private ActionBarDrawerToggle mDrawerToggle; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 


     mDrawLayout=(DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerToggle=new ActionBarDrawerToggle(this,mDrawLayout,R.string.drawer_opened,R.string.drawer_closed){ 
      @Override 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       if(getSupportActionBar()!=null) 
       getSupportActionBar().setTitle(R.string.drawer_opened); 
      } 

      @Override 
      public void onDrawerClosed(View drawerView) { 
       super.onDrawerClosed(drawerView); 
       if(getSupportActionBar()!=null) 
        getSupportActionBar().setTitle(R.string.drawer_closed); 
      } 
     }; 
     getSupportActionBar().setDisplayShowCustomEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 

     mDrawLayout.addDrawerListener(mDrawerToggle); 
    // mDrawLayout.setDrawerListener(mDrawerToggle); 

    } 

    @Override 
    public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) { 
     super.onPostCreate(savedInstanceState, persistentState); 
     mDrawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     if(mDrawerToggle.onOptionsItemSelected(item)) 
      return true; 


     return super.onOptionsItemSelected(item); 

    } 
} 
+1

嘗試定義onCreateOptionMenu做出了menu.xml文件文件,其中你必須包含你需要在導航中顯示的東西菜單 – Swr7der

回答

0

[activity_main.xml中]

<FrameLayout android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/fl_activity_main_container" /> 

<ListView android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:id="@+id/lv_activity_main_nav_list" 
    android:layout_gravity="start" 
    android:choiceMode="singleChoice" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="0dp" 
    android:background="#EDEDED"/> 

更多的代碼,這個網址 - http://androidhuman.com/524