当与Drawerlayout

时间:2018-02-01 19:41:08

标签: android visual-studio xamarin xamarin-studio

我正在使用Xamarin并使用左导航抽屉创建应用程序,我想要一个带有drawerlayout的BottomNavigationView,但是当我运行应用程序时,只有抽屉布局显示,BottomNavigationView没有显示。但是当我在没有DrawerLayout的情况下使用它时效果很好。我附上我的xml代码,你能告诉我我哪里错了吗?在主要活动中,我用FindviewbyId<>来修复BottomnavigationBar。并将navigationItemSelected方法添加到BottomnavigationBar。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.support.v7.widget.Toolbar
        android:id="@+id/toolbar3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    <android.support.v4.widget.DrawerLayout
        android:layout_below="@id/toolbar3"
        android:id="@+id/drawerlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <!--Main Layout-->

        <RelativeLayout
            android:layout_below="@id/drawerlayout"
            android:id="@+id/mainlayout2"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
          <android.support.design.widget.BottomNavigationView
                android:layout_below="@id/drawerlayout"
                android:id="@+id/bottom_navigation"
                android:layout_alignParentBottom="true"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:itemBackground="@color/bottom_navigation"
                app:itemIconTint="@drawable/bottom_nav_color_state"
                app:itemTextColor="@drawable/bottom_nav_color_state"
                app:menu="@menu/bottom_nav" />
        </RelativeLayout>

      <!--Left Drawer-->

        <ListView
            android:layout_below="@id/mainlayout2"
            android:layout_marginLeft="10dp"
            android:id="@+id/listview"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:choiceMode="singleChoice"
            android:divider="#003160"
            android:dividerHeight="1dp"
            android:background="#003160" />
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

如果您希望NavigationView充当抽屉,则根视图应为<android.support.v4.widget.DrawerLayout>,而不是<RelativeLayout>。有关详细信息,请参阅此official document及其sample

像这样修改你的代码:

<?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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

  <!-- The main content view -->
  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/toolbar_layout">
      <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways" />
    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:itemBackground="@android:color/black"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        app:menu="@menu/navigation" />
  </RelativeLayout>

  <!--Left Drawer-->
  <android.support.design.widget.NavigationView
      android:id="@+id/nav_view"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_gravity="left"
      app:headerLayout="@layout/nav_header"
      app:menu="@menu/nav_menu"
      android:fitsSystemWindows="true" />

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

Effect