导航抽屉不适合状态栏下方(android studio)[重复]

Posted

技术标签:

【中文标题】导航抽屉不适合状态栏下方(android studio)[重复]【英文标题】:Navigation Drawer does not fit underneath the status bar (android studio) [duplicate] 【发布时间】:2016-12-04 21:02:40 【问题描述】:

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

Navdrawer1

我希望它看起来像这样:

Navdrawer2

这是我的 xml 代码:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
android:fitsSystemWindows="true"
android:orientation="vertical"
>


<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_

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

    android:layout_
    android:id="@+id/drawerLayout"
    >




<FrameLayout
    android:orientation="vertical"
    android:layout_
    android:layout_

    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_
    android:layout_

    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>

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

【问题讨论】:

【参考方案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_
    android:layout_
    android:fitsSystemWindows="true"//fit to top status bar
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_home_actvity"
        android:layout_
        android:layout_ />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_
        android:layout_
        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_
    android:layout_
    android:fitsSystemWindows="true" //fit to top status bar
    tools:context=".activity.HomeActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_
        android:layout_
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_
            android:layout_
            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>

【讨论】:

工具栏中的背景是多余的。纯色背景和高度由 AppBarLayout 提供。 抱歉,没有收到您的评论【参考方案2】:

您必须在“NavigationView”中包含您的工具栏

您的代码将如下所示:

<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_
    android:layout_
    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_
        android:layout_>

            <android.support.v7.widget.Toolbar
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_
                android:layout_

                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_
        android:layout_
        android:layout_gravity="left"
        android:fitsSystemWindows="true">

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

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

【讨论】:

*在DrawerLayout内,工具栏高度应为?actionBarSize 英文不太懂,但是工具栏等组件必须在“CoordinatorLayout”之内。如果有一个外部组件“DrawerLayout”,只需可视化即可。尝试将视图的所有组件放在“CoordinatorLayout”中,这应该可以解决您的问题。 我知道,但请修正答案中的第一句话。【参考方案3】:

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_
    android:layout_
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    android:elevation="7dp">

    <LinearLayout
    android:layout_
    android:layout_
    android:orientation="vertical">

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

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

    </LinearLayout>



    <android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_
    android:layout_
    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_
    android:background="@drawable/back_header"
    android:layout_>


    <TextView
    android:layout_
    android:layout_
    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();
    

在 mainactivity 中更改为你的编码

【讨论】:

以上是关于导航抽屉不适合状态栏下方(android studio)[重复]的主要内容,如果未能解决你的问题,请参考以下文章

Android - 为带有导航抽屉和应用栏的应用完全隐藏状态和导航栏

导航抽屉在状态栏上半透明不起作用

牛轧糖状态栏上的导航抽屉?

侧面导航抽屉项目单击不起作用

工具栏下方的导航抽屉

导航抽屉操作栏按钮不起作用