导航抽屉不适合状态栏(android studio)

时间:2016-07-30 05:49:44

标签: android android-studio navigation-drawer

我正在开发android studio中的应用程序。我正在尝试创建一个适合状态栏正下方的导航抽屉。出于某种原因,导航栏显示在应用栏下方,而不是像这张图片中那样:

Navdrawer1

enter image description here

我希望它看起来像这样:

Navdrawer2

enter image description here

这是我的xml代码:

<LinearLayout
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:orientation="vertical"
>


<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:background="#ff4000"
    android:id="@+id/toolbar"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:title="Home"
    />

<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_height="match_parent"
    android:layout_gravity="start"

    android:layout_width="match_parent"
    android:id="@+id/drawerLayout"
    >




<FrameLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:id="@+id/containerView">
 </FrameLayout>



<android.support.design.widget.NavigationView

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"

    android:fitsSystemWindows="true"
    android:layout_gravity="start"
    android:background="#FFFFFF"
    app:headerLayout="@layout/navigation_drawer_header"
    android:id="@+id/shitstuff"
    app:itemTextColor="@color/black"
    app:menu="@menu/drawermenu"
    android:layout_marginTop="-24dp"


    />



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

</LinearLayout>

任何帮助都会很棒!谢谢!

3 个答案:

答案 0 :(得分:1)

这里需要工具栏:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"//fit to top status bar
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_home_actvity"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start">

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

app_bar_home_actvity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" //fit to top status bar
    tools:context=".activity.HomeActivity">

    <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_home_actvity" />


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

答案 1 :(得分:0)

您必须将工具栏包含在&#34; NavigationView&#34;

您的代码看起来像这样:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            <android.support.v7.widget.Toolbar
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:background="#ff4000"
                android:id="@+id/toolbar"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:title="Home"
                />


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


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:fitsSystemWindows="true">

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

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

答案 2 :(得分:0)

<强> activity_main_nav.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    android:elevation="7dp">

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

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

    <!-- Let's add fragment -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frame"/>

    </LinearLayout>



    <android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/activity_home_drawer"

    app:headerLayout="@layout/header"/>


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

header.xml

    <?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:background="@drawable/back_header"
    android:layout_height="178dp">


    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:text="Header"
    android:textColor="@android:color/white"/>

</LinearLayout>

菜单文件夹activity_home_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
   <menu xmlns:android="http://schemas.android.com/apk/res/android">

  <group android:id="@+id/grp1"  android:checkableBehavior="single">
    <item
        android:id="@+id/nav_first"
        android:icon="@drawable/first"
        android:title="First" />

   </group>

    // if you want to underline than use group

    <item
        android:id="@+id/nav_second"
        android:icon="@drawable/second"
        android:title="second" />


      </menu>

MainActivity.java

  public class MainActivity extends AppCompatActivity {


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



    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().replace(R.id.frame, new YourFragment()).commit();

    }
    setNavigationDrawer();
    setToolBar();




}

private void setToolBar() {
    final Toolbar tb = (Toolbar) findViewById(R.id.toolbar1);
    setSupportActionBar(tb);

    ActionBar ab = getSupportActionBar();
    ab.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
    ab.setDisplayHomeAsUpEnabled(true);
    ab.setDisplayShowHomeEnabled(true);


    final android.app.FragmentManager fm = getFragmentManager();

    fm.addOnBackStackChangedListener(new android.app.FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {

            if (getSupportFragmentManager().getBackStackEntryCount() ==0) {
                dLayout.closeDrawers();
                finish();
            }
            else
            {
                dLayout.closeDrawers();

            }
        }
    });
}




private void setNavigationDrawer() {
    dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    NavigationView navView = (NavigationView) findViewById(R.id.navigation);
    Menu m = navView.getMenu();
    for (int i=0;i<m.size();i++) {
        MenuItem mi = m.getItem(i);

        //for aapplying a font to subMenu ...
        SubMenu subMenu = mi.getSubMenu();
        if (subMenu!=null && subMenu.size() >0 ) {
            for (int j=0; j <subMenu.size();j++) {
                MenuItem subMenuItem = subMenu.getItem(j);


            }
        }


    }
    navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            Fragment frag = null;
            int itemId = menuItem.getItemId();

            if (itemId == R.id.first) {
               // frag = new First();
                frag = new SearchJobActivity();

            } else if (itemId == R.id.second) {
                frag = new second();
            }


            if (frag != null) {
                openFragmentNew(frag);
                dLayout.closeDrawers();
                return  true;
            }


            return false;
        }
    });
}


public void openFragmentNew(Fragment fragment) {
    String backStateName = fragment.getClass().getName();
    FragmentManager manager = getSupportFragmentManager();
    //fragment not in back stack, create it.
    FragmentTransaction ft = manager.beginTransaction();
    if(!fragments.contains(backStateName)) {

       // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        // ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
        ft.replace(R.id.frame, fragment);
        ft.addToBackStack(backStateName);
        ft.commit();


        System.out.println("backStateName" + fragments);
    }
    else
    {
       // ft.remove(fragment);

        ft.replace(R.id.frame, fragment);
        ft.commit();
  //      manager.popBackStack();
    }


}



@Override
public void onBackPressed() {
    if (dLayout.isDrawerOpen(GravityCompat.START)) {
        dLayout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

在主要活动中改变你的身份