带标题的Xamarin android弹出菜单

时间:2018-04-30 21:47:30

标签: android xamarin menu popup

我尝试制作简单的弹出菜单,但我无法在顶部添加未点击的标题。 EX: 我的标题   P1   P2   P3

我的xml是:

<?xml version="1.0" encoding="utf-8" ?>
<!--For all properties see: http://developer.android.com/guide/topics/resources/menu-resource.html-->

  <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/key7" android:title="p1" showAsAction="always" />
    <item android:id="@+id/key6" android:title="p2" showAsAction="always" />
    <item android:id="@+id/key6" android:title="p3" showAsAction="always" />
  </menu>

我的代码是

PopupMenu menu = new PopupMenu(this, ChangeKeyButton);
menu.Inflate(Resource.Menu.menu_tonacja);
menu.MenuItemClick += Menu_MenuItemClick;
menu.Show();

我花了很多时间才找到这个没有结果的解决方案 感谢

1 个答案:

答案 0 :(得分:0)

似乎弹出菜单无法设置标题。我认为您可以尝试使用PopupWindow代替 例如:

    Button button = FindViewById<Button>(Resource.Id.button1);
    button.Click += (s, arg) => {            
        TextView textView = new TextView(this);
        textView.Text = "header";

        ListView listView = new ListView(this);
        listView.AddHeaderView(textView);                 
        var items = new string[] { "menu1", "menu2", "menu3" };
        listView.Adapter = new ArrayAdapter<string>(this, Resource.Layout.listitem, items);
        listView.ItemClick += List_ItemClick;

        PopupWindow popupWindow = new PopupWindow(this);
        popupWindow.Width = ViewGroup.LayoutParams.WrapContent;
        popupWindow.Height = ViewGroup.LayoutParams.WrapContent;
        popupWindow.ContentView = listView;               
        popupWindow.OutsideTouchable = false;
        popupWindow.Focusable=true;               
        popupWindow.ShowAsDropDown(button);
    };

listitem.axml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView  
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:text="Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1" />