导航抽屉的实现

时间:2019-08-29 12:02:18

标签: android xamarin xamarin.android

我是Xamarin的新手,我正在尝试对现有活动实施导航抽屉。

现有活动的顶部有一个操作栏,而Frgament容器则包含底部的导航按钮。该片段已填充到活动的Oncreate方法中。

代码是这样的。

TestActivity.cs

  protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SupportActionBar.SetDisplayShowTitleEnabled(false);
        SupportActionBar.SetDisplayShowHomeEnabled(false);

        SupportActionBar.SetCustomView(Resource.Layout.NavigationBar);
        SupportActionBar.SetDisplayShowCustomEnabled(true);
        SetContentView(Resource.Layout.FragmentContainer);

        Support.V7.Widget.Toolbar toolbar = FindViewById<Support.V7.Widget.Toolbar>(Resource.Id.toolbar);

        DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        Support.V7.App.ActionBarDrawerToggle toggle = new Support.V7.App.ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);
        drawer.AddDrawerListener(toggle);

        toggle.SyncState();

        NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
        navigationView.SetNavigationItemSelectedListener(this);

        UpdateFragment(new TestFragment());
    }

Frgamentcontainer.axml(的某些部分)

  <RelativeLayout      xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:id="@+id/containerMain"
   android:minWidth="25px"
   android:minHeight="25px"
   style="@style/ActivityLayout">

<LinearLayout
    android:orientation="vertical"
    android:minWidth="25px"
    android:layout_marginBottom="1dp"
    android:minHeight="25px"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/bottomBar"
    android:id="@+id/container" />
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="5dp"
    android:id="@+id/status"
    android:background="#2980b9"
    android:layout_alignParentBottom="true" />

我想用带有汉堡包图标的工具栏的NavigationDrawer替换现有的支持操作栏。

我在下面尝试过这样

受保护的覆盖无效OnCreate(捆绑包)         {             base.OnCreate(bundle);

        SupportActionBar.SetDisplayShowTitleEnabled(false);
        SupportActionBar.SetDisplayShowHomeEnabled(false);

        SupportActionBar.SetCustomView(Resource.Layout.NewDrwerLayout);
     }

NewDrawerLayout.axml

<?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="fill_parent"
                                    android:layout_height="fill_parent"
                                    android:fitsSystemWindows="true"
                                    tools:openDrawer="start">
  <include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

  <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@drawable/activities" />

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

app_bar_main.axml

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

,然后在TestActivity.cs中,如下所示:

Support.V7.Widget.Toolbar toolbar = FindViewById<Support.V7.Widget.Toolbar>(Resource.Id.toolbar);

            DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
            Support.V7.App.ActionBarDrawerToggle toggle = new Support.V7.App.ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);
            drawer.AddDrawerListener(toggle);

            toggle.SyncState();

            NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
            navigationView.SetNavigationItemSelectedListener(this);

After all the above changes I could see the toolbar with hamburger but navigation drawer panel is cropping and showing the content only in the supportActionBar region. 

Please help me.

TestActivity.cs

 protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SupportActionBar.SetDisplayShowTitleEnabled(false);
            SupportActionBar.SetDisplayShowHomeEnabled(false);

            SupportActionBar.SetCustomView(Resource.Layout.NavigationBar);
            SupportActionBar.SetDisplayShowCustomEnabled(true);
            SetContentView(Resource.Layout.FragmentContainer);

            Support.V7.Widget.Toolbar toolbar = FindViewById<Support.V7.Widget.Toolbar>(Resource.Id.toolbar);

            DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
            Support.V7.App.ActionBarDrawerToggle toggle = new Support.V7.App.ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);
            drawer.AddDrawerListener(toggle);

            toggle.SyncState();

            NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
            navigationView.SetNavigationItemSelectedListener(this);

            UpdateFragment(new TestFragment());
        }


Frgamentcontainer.axml(Some portions of)

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/containerMain"
    android:minWidth="25px"
    android:minHeight="25px"
    style="@style/ActivityLayout">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="65dp"
        android:id="@+id/bottomBar"
        android:layout_alignParentBottom="true">
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="65dp"
            android:layout_gravity="top"
            android:background="#00000000"
            android:orientation="horizontal"
            android:padding="2dp">
            <ImageView
                android:layout_gravity="center"
                android:src="@drawable/swhlogo5"
                android:id="@+id/logo"
                style="@style/LogoIcon"
                android:padding="10dp"
                android:layout_centerVertical="true" />
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:layout_marginLeft="100dp"
                android:layout_marginRight="50dp"
                android:id="@+id/textRdName"
                android:padding="10dp"
                android:singleLine="true"
                android:ellipsize = "end" 
                style="@style/SmallText"
                android:layout_centerVertical="true"
                android:textColor="@android:color/white" />
            <ImageView
                android:background="@drawable/info"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:padding="10dp"
                android:id="@+id/imgInfo"
                android:layout_centerVertical="true"
                android:layout_alignParentRight="true" />
        </RelativeLayout>
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:minWidth="25px"
        android:layout_marginBottom="1dp"
        android:minHeight="25px"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/bottomBar"
        android:id="@+id/container" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="5dp"
        android:id="@+id/status"
        android:background="#2980b9"
        android:layout_alignParentBottom="true" />

在完成所有上述更改之后,我可以看到带有汉堡包的工具栏,但是导航抽屉面板正在裁剪并且仅在supportActionBar区域中显示内容。

请帮助我。

0 个答案:

没有答案