-3

Hier ist meine Haupt-XML-Datei:Wie starte ich Drawer Layout unterhalb der Toolbar in Android?

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.v4.widget.DrawerLayout 
     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="match_parent" 
     android:fitsSystemWindows="true" 
     android:id="@+id/nav_drawer"> 

    <android.support.design.widget.CoordinatorLayout 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     tools:context="com.example.amit.rssreader.MainActivity"> 


     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 

     </android.support.design.widget.AppBarLayout> 

     <include layout="@layout/content_main" /> 


    </android.support.design.widget.CoordinatorLayout> 
     <android.support.design.widget.NavigationView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      app:menu="@menu/navigation_menu" 
      android:layout_gravity="start"> 

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

Dies ist, was This is what is happening

ich .Dieses war nicht eine Navigations Schublade Aktivität, die Navigationsleiste wurde die Schublade unter der Aktionsleiste haben möge geschieht später von xml hinzugefügt.

Antwort

1

Ich denke, das dir helfen kann:

  1. das XML-Layout ändern:

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
        <android.support.design.widget.AppBarLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"> 
         <android.support.v7.widget.Toolbar 
          android:id="@+id/toolbar" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"> 
    
         </android.support.v7.widget.Toolbar> 
        </android.support.design.widget.AppBarLayout> 
    
        <android.support.v4.widget.DrawerLayout 
         android:layout_width="match_parent" 
         android:layout_height="match_parent"> 
         <android.support.design.widget.CoordinatorLayout 
          android:layout_width="match_parent" 
          android:layout_height="match_parent"> 
          <include layout="@layout/content_main" /> 
         </android.support.design.widget.CoordinatorLayout> 
        </android.support.v4.widget.DrawerLayout> 
    </LinearLayout> 
    
  2. für Aktivität ohne Symbolleiste erstellen Thema in Werten/styles.xml und es auf Ihre Tätigkeit in AndroidManifest-Datei:

    <style name="AppTheme.NoActionBar"> 
        <item name="windowActionBar">false</item> 
        <item name="windowNoTitle">true</item> 
    </style> 
    
  3. Und damit es funktioniert, machen Die onCreate-Methode Ihrer Aktivität sieht so aus:

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
        final DrawerLayout drawerLayout = (DrawerLayout) 
         findViewById(R.id.drawerLayout); 
    
        ActionBar supportActionBar = getSupportActionBar(); 
        if(supportActionBar != null) { 
         supportActionBar.setDisplayHomeAsUpEnabled(true); 
         supportActionBar.setHomeButtonEnabled(true); 
        } 
    
        setSupportActionBar(toolbar); 
    
        final ListView drawerList = (ListView) 
         findViewById(R.id.drawerList); 
         drawerList.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, 
         new String[] { "Item 1", "Item 2", "Item 3" })); 
    
    
    
         drawerLayout.setScrimColor(Color.TRANSPARENT); 
    
         ActionBarDrawerToggle actionBarDrawerToggle = 
         new ActionBarDrawerToggle(this, drawerLayout, 
           toolbar, R.string.app_name, R.string.app_name); 
    
         actionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
          if(drawerLayout.isDrawerOpen(drawerList)) { 
           drawerLayout.closeDrawers(); 
          } else { 
           drawerLayout.openDrawer(drawerList); 
          } 
         } 
        }); 
    
        drawerLayout.addDrawerListener(actionBarDrawerToggle); 
    
        actionBarDrawerToggle.syncState(); 
    } 
    

    Und so funktioniert es. Drawer closedDrawer opened

Verwandte Themen