我正在使用NavigationView从以下教程制作简单的菜单: http://maximeesprit.com/en/xamarin-android-actionbar-en/
但我有问题,以下菜单项不发送事件
我的菜单如下:
我的main.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/maintActivity_drawerlayout">
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/maintActivity_navigationView"
app:menu="@menu/left_menus" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="240dp"
android:orientation="vertical"
android:gravity="bottom"
android:layout_marginLeft="16dp"
android:id="@+id/container_layout">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frameLayout_viewForFragments" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
我的mainActivity.cs文件:
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using Android.Views;
using Android.Support.V4.Widget;
using Android.Support.Design.Widget;
using Android.Support.V4.View;
// install from NuGet android.support.design
// And App Compat support
namespace App4
{
[Activity(Label = "TopSideBar App", MainLauncher = true , Icon = "@drawable/ic_launcher")]
public class MainActivity : AppCompatActivity
{
DrawerLayout m_drawerLayout;
NavigationView m_navigationView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
SupportActionBar.Title = Resources.GetString(Resource.String.app_name); ;
m_drawerLayout = FindViewById<DrawerLayout>(Resource.Id.maintActivity_drawerlayout);
m_navigationView = FindViewById<NavigationView>(Resource.Id.maintActivity_navigationView);
m_navigationView.NavigationItemSelected += M_navigationView_NavigationItemSelected;
//Display sandwich menu icon
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu_black_24dp_1x);
ConfigureNavigationViewHeader();
}
private void ConfigureNavigationViewHeader()
{
View viewHeader = LayoutInflater.Inflate(Resource.Layout.mainActivity_navigationView_header, null);
ImageView imageviewUserAvatar = viewHeader.FindViewById<ImageView>(Resource.Id.mainActivity_navigationView_header_imageview_userAvatar);
TextView textviewUserPseudo = viewHeader.FindViewById<TextView>(Resource.Id.mainActivity_navigationView_header_textview_UserPseudo);
TextView textviewUserDesc = viewHeader.FindViewById<TextView>(Resource.Id.mainActivity_navigationView_header_textview_UserDesc);
imageviewUserAvatar.SetImageResource(Resource.Drawable.profile_pic);
textviewUserPseudo.Text = "Housma Mardini";
textviewUserDesc.Text = "Web Developer";
m_navigationView.AddHeaderView(viewHeader);
}
private void M_navigationView_NavigationItemSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
{
switch (e.MenuItem.ItemId)
{
case Resource.Id.leftmenu_profile:
Toast.MakeText(this, "profile", ToastLength.Short).Show();
m_drawerLayout.CloseDrawer(GravityCompat.Start);
break;
case Resource.Id.leftmenu_shopping_list:
break;
case Resource.Id.leftmenu_favorites:
break;
case Resource.Id.leftmenu_params_option:
break;
case Resource.Id.leftmenu_params_about:
break;
}
}
public override bool OnCreateOptionsMenu(Android.Views.IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.top_menus, menu);
return base.OnCreateOptionsMenu(menu);
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Resource.Id.share:
Toast.MakeText(this , "Share" ,ToastLength.Short ).Show();
break;
case Resource.Id.moreoption:
Toast.MakeText(this, "Settings", ToastLength.Short).Show();
break;
//Handle sandwich menu icon click
case Android.Resource.Id.Home:
//If menu is open, close it. Else, open it.
if (m_drawerLayout.IsDrawerOpen(GravityCompat.Start))
m_drawerLayout.CloseDrawers();
else
m_drawerLayout.OpenDrawer(GravityCompat.Start);
break;
}
return base.OnOptionsItemSelected(item);
}
}
}
但是当我从main.xml文件中删除LinearLayout和FrameLayout标记时,每件事情都运行良好
答案 0 :(得分:1)
正如邮件所讨论的,这是答案:
您的活动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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/maintActivity_drawerlayout">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frameLayout_viewForFragments" />
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/maintActivity_navigationView"
app:menu="@menu/left_menus" />
</android.support.v4.widget.DrawerLayout>