0

大家好,抽屜式導航欄是工具欄下在Android中

我初來乍到的Android編程和目前工作的一個實踐的應用程序,或多或少的教程代碼拼湊而成。

現在我有導航工具欄下方的抽屜。我想重新調整xml層次結構,使其與Material Design指南保持一致,並在工具欄上方具有導航工具欄。這似乎是一個足夠簡單的任務,但對於我的生活,我似乎無法得到它。

任何人都可以提供任何建議嗎?

<?xml version="1.0" encoding="utf-8"?> 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:apps="http://schemas.android.com/apk/res-auto" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:orientation="vertical" 
 
    android:background="@color/background_color"> 
 

 
    <android.support.v7.widget.Toolbar 
 
     xmlns:android="http://schemas.android.com/apk/res/android" 
 
     android:id="@+id/toolbar" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:background="@color/primary_color" 
 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
 
     apps:title="@string/app_name"/> 
 

 
    <android.support.v4.widget.DrawerLayout 
 
     xmlns:android="http://schemas.android.com/apk/res/android" 
 
     xmlns:apps="http://schemas.android.com/apk/res-auto" 
 
     android:id="@+id/drawer_layout" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent"> 
 

 
     <FrameLayout 
 
      android:id="@+id/frame_layout" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="match_parent" 
 
      android:orientation="vertical"> 
 
     </FrameLayout> 
 

 
     <android.support.design.widget.NavigationView 
 
      xmlns:android="http://schemas.android.com/apk/res/android" 
 
      xmlns:apps="http://schemas.android.com/apk/res-auto" 
 
      android:id="@+id/navigation_view" 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="match_parent" 
 
      android:layout_gravity="start" 
 
      apps:itemTextColor="#000" 
 
      android:fitsSystemWindows="true" 
 
      apps:headerLayout="@layout/navigation_header" 
 
      apps:menu="@menu/drawer_menu"/> 
 

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

 
</LinearLayout>

package android.dohyun.projectannie; 
 

 
import android.os.Bundle; 
 
import android.support.design.widget.NavigationView; 
 
import android.support.v4.app.FragmentManager; 
 
import android.support.v4.app.FragmentTransaction; 
 
import android.support.v4.widget.DrawerLayout; 
 
import android.support.v7.app.ActionBarDrawerToggle; 
 
import android.support.v7.app.AppCompatActivity; 
 
import android.support.v7.widget.Toolbar; 
 
import android.view.MenuItem; 
 

 
public class MainActivity extends AppCompatActivity { 
 

 
    DrawerLayout mDrawerLayout; 
 
    NavigationView mNavigationView; 
 
    FragmentManager mFragmentManager; 
 
    FragmentTransaction mFragmentTransaction; 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 

 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
 
     mNavigationView = (NavigationView) findViewById(R.id.navigation_view); 
 

 

 

 
     mFragmentManager = getSupportFragmentManager(); 
 
     mFragmentTransaction = mFragmentManager.beginTransaction(); 
 
     mFragmentTransaction.replace(R.id.frame_layout, new NotesFragment()).commit(); 
 

 
     mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
 
      @Override 
 
      public boolean onNavigationItemSelected(MenuItem menuItem) { 
 
       mDrawerLayout.closeDrawers(); 
 

 
       if(menuItem.getItemId() == R.id.menu_notes) { 
 
        FragmentTransaction fragmentTransactionNotes = mFragmentManager.beginTransaction(); 
 
        fragmentTransactionNotes.replace(R.id.frame_layout, new NotesFragment()).commit(); 
 
       } 
 
       if(menuItem.getItemId() == R.id.menu_folders) { 
 
        FragmentTransaction fragmentTransactionFolders = mFragmentManager.beginTransaction(); 
 
        fragmentTransactionFolders.replace(R.id.frame_layout, new FoldersFragment()).commit(); 
 
       } 
 
       if(menuItem.getItemId() == R.id.menu_trash) { 
 
        FragmentTransaction fragmentTransactionTrash = mFragmentManager.beginTransaction(); 
 
        fragmentTransactionTrash.replace(R.id.frame_layout, new TrashFragment()).commit(); 
 
       } 
 
       if(menuItem.getItemId() == R.id.menu_settings) { 
 
        FragmentTransaction fragmentTransactionSettings = mFragmentManager.beginTransaction(); 
 
        fragmentTransactionSettings.replace(R.id.frame_layout, new SettingsFragment()).commit(); 
 
       } 
 
       if(menuItem.getItemId() == R.id.menu_info) { 
 
        FragmentTransaction fragmentTransactionInfo = mFragmentManager.beginTransaction(); 
 
        fragmentTransactionInfo.replace(R.id.frame_layout, new InfoFragment()).commit(); 
 
       } 
 

 
       return false; 
 
      } 
 
     }); 
 

 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
 
     ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name, R.string.app_name); 
 

 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 
 
     mDrawerToggle.syncState(); 
 

 
    } 
 
}

上面的代碼是一個在這裏發現,由拉坦寫的稍微修改的版本: ​​

回答

0

DrawerLayout是一個特殊的支持導航抽屜的。

沒有layout_gravity的子視圖是主視圖。子例如有layout_gravity例如android:layout_gravity="start"是導航抽屜。

這意味着DrawerLayout應該是頂級視圖組。換句話說,把你的佈局與ToolbarDrawerLayout