【问题标题】:Xamarin android. Open right navigation bar with button from custom toolbarXamarin 安卓。使用自定义工具栏中的按钮打开右侧导航栏
【发布时间】:2018-04-12 15:20:00
【问题描述】:

在我的 xamarin android 应用程序中,我有一个正确的导航栏和一个自定义工具栏。导航栏的代码是这样的

<android.support.design.widget.NavigationView
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout_gravity="right"
 android:id="@+id/right_nav_view"
 app:menu="@menu/menu_navigation_swipe" />

工具栏的代码是这样的

<android.support.v7.widget.Toolbar
 android:id="@+id/toolbar"
 android:layout_width="match_parent"
 android:layout_height="45dp"
 android:minHeight="?attr/actionBarSize"
 android:background="@drawable/rectangle"
 android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
 app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
  <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/HomeButton"
        android:layout_gravity="center"
        android:src="@drawable/logo"
        android:clickable="true" />

  <ImageView
          android:padding="10dp"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/right_nav"
         android:layout_alignParentEnd="true"
         android:layout_alignParentRight="true"
         android:src="@drawable/icon_settings"
         android:clickable="true" />

    </RelativeLayout>
</android.support.v7.widget.Toolbar>

我想在用户按下 id = right_nav 的 ImageView 时打开导航栏。我该怎么做?

【问题讨论】:

    标签: android xamarin.android navigation-drawer


    【解决方案1】:

    Xamarin 安卓。使用自定义工具栏中的按钮打开右侧导航栏

    将您的NavigationView 放入DrawerLayout,例如:

    <?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_height="match_parent"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true">
    
      <!-- I place your Toolbar in this include_list_viewpager layout -->
      <include layout="@layout/include_list_viewpager"/>
    
      <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_gravity="right"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/drawer_view"/>
    
    </android.support.v4.widget.DrawerLayout>
    

    然后,正如@Federico Navarrete 指出的那样,为您的ImageView 定义一个点击事件:

    DrawerLayout drawerLayout = FindViewById<DrawerLayout> (Resource.Id.drawer_layout);
    
    ImageView right_nav = FindViewById<ImageView>(Resource.Id.right_nav);
    
    right_nav.Click += (s, e) =>
    {
         drawerLayout.OpenDrawer(Android.Support.V4.View.GravityCompat.End);
    };
    

    【讨论】:

    • 感谢您的回答。是否可以有左右 NavigationView 和两个不同的按钮来打开它们?
    • @KonstantinosEvangelidis,是的,你可以参考这个例子:NavigationViewOnBothsSides
    • 非常感谢您的链接。
    • @KonstantinosEvangelidis,你的问题解决了吗?
    • @KonstantinosEvangelidis,如果这对您有帮助,您能否将其标记为答案?这对我们很重要。谢谢!
    【解决方案2】:

    试试这个

    ImageView right_nav = (ImageView)findViewById(R.id.right_nav);
            right_nav.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    drawer.openDrawer(Gravity.RIGHT);
                }
            });
    

    【讨论】:

      【解决方案3】:

      您可以使用此代码从右侧打开它:

      //You get your navigation drawer
      var drawer = FindViewById<DrawerLayout>(Resource.Id.right_nav_view);
      //You get the image view
      var right_nav = FindViewById<ImageView>(Resource.Id.right_nav);
      
      //You define the click event
      right_nav.Click += (s, e) =>
      {
          //You open the navigation Drawer from the right
          drawer.OpenDrawer(GravityCompat.Right);
      }
      

      【讨论】:

      • 感谢您的回答。问题是我是 NavigationView 而不是 DrawerLayout。 NavigationView 没有 .OpenDrawer 方法来激活它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      • 2020-11-11
      • 2017-12-30
      • 2014-02-27
      • 2011-09-04
      • 1970-01-01
      相关资源
      最近更新 更多