【问题标题】:Navigation drawer still not appearing over toolbar导航抽屉仍然没有出现在工具栏上
【发布时间】:2016-04-19 13:21:36
【问题描述】:

我正在关注一本面向初学者的 Android 书籍,尽管它很新,但在某些领域仍然过时。我正在制作我的第一个带有导航抽屉的应用程序,并且发现它用于实现导航抽屉的方法已被取代。

使用this answer 中的技术我已经取得了一定的成功——在导航抽屉中的顶部项目完全被工具栏遮挡之前,现在它只是部分被遮挡了——但我希望导航抽屉出现上方工具栏完全。我尝试过移动一些 XML,但无济于事。这是我的布局代码:

content_main.xml

<RelativeLayout 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"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                tools:context=".MainActivity">

    <!-- Layout to hold fragments loaded by navdrawer-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity"
        android:background="#ffffffff"
        android:id="@+id/fragmentHolder">
    </RelativeLayout>

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

        <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/nav" /> <!-- The items to display -->

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


</RelativeLayout>

activity_main.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"
    tools:context="com.interglobalmegacorp.whereitssnap.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.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

会发生以下情况: Screenshot link because I don't have enough rep to embed yet

我唯一的想法是它可能与当前设置为 NoActionBar 的主题有关,因为其他任何东西似乎都会使应用程序崩溃:

This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

我做错了什么?

编辑 onCreate() 方法:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mNavigationView = (NavigationView) findViewById(R.id.navigation);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); // bind the layout
        mActivityTitle = getTitle().toString(); // get the title of the app and store it


        // set up the drawer with our purpose-built method
        setupDrawer();

        // set up the nav drawer toggle
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        // listen for clicks on navdrawer
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                switchFragment(item);
                return true;
            }
        });

        // load default fragment
        Fragment fragment = new TitlesFragment();
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.fragmentHolder, fragment, "TITLES").commit();
    }

【问题讨论】:

  • 这不是你的主题。这只是布局。您希望DrawerLayout 作为最外层的View。那么CoordinatorLayout 应该是DrawerLayout 的第一个孩子,而您的fragmentHolder RelativeLayout 将代替&lt;include&gt;。另一个RelativeLayout 是不必要的。

标签: android android-layout navigation


【解决方案1】:

根据指南,您的 Drawerlayout 应该包含您的 Coordinatorlayout

关于代码中的第一个 RealativeLayout 你应该删除它。

试试这个:

content_main.xml

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

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

    <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/nav" /> <!-- The items to display -->

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

activity_main.xml

<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"
    tools:context="com.interglobalmegacorp.whereitssnap.MainActivity">

    <!-- Layout to hold fragments into activity-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity"
        android:background="#ffffffff"
        android:id="@+id/fragmentHolder">
    </RelativeLayout>

    <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>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mNavigationView = (NavigationView) findViewById(R.id.navigation);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); // bind the layout
    mActivityTitle = getTitle().toString(); // get the title of the app and store it

    final ActionBar ab = getSupportActionBar();
    if (ab != null) {
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setHomeButtonEnabled(true);
    }

    mDrawerToggle = new ActionBarDrawerToggle(
            this, mDrawerLayout, mToolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    mDrawerLayout.addDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();

    navigationView.setNavigationItemSelectedListener(
        new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                switchFragment(item);
                return true;
            }
        });

    // load default fragment
    Fragment fragment = new TitlesFragment();
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.fragmentHolder, fragment, "TITLES").commit();

}

【讨论】:

  • 我应该把实际保存 NavDrawer 加载的 Fragments 的 RelativeLayout(我的 id fragHolder)放在哪里?
  • 您可以将 app:headerLayout="@layout/nav_header" 添加到导航视图中
  • 对不起,这对我来说毫无意义。我已经尝试使用上面的代码,它基本上导致应用程序崩溃 - MainActivity 代码无法再找到它需要的各种布局元素,例如导航视图。
  • 发布你的 onCreate() 方法。
  • @moralesbatovski “导航抽屉加载的片段”是指应用程序的主要部分,即应用程序的实际“页面”,而不是导航抽屉本身中的项目
猜你喜欢
  • 2019-09-15
  • 2015-02-26
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-31
  • 1970-01-01
相关资源
最近更新 更多