简体   繁体   English

如何在 Xamarin Android Fragment 中加载工具栏布局

[英]How to load Toolbar layout in Xamarin Android Fragment

I'm trying to create a basic toolbar that I can place clickable icons on.我正在尝试创建一个可以放置可点击图标的基本工具栏。 This will be inside a separate fragment to the starting activity page.这将位于起始活动页面的单独片段内。

Fragment Code:片段代码:

 public void onCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetHasOptionsMenu(true);
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        base.OnCreateView(inflater, container, savedInstanceState);

        //Inflates the new xml into the fragment 
        var view = inflater.Inflate(Resource.Layout.chord_progression, container, false);
        mToolbar = view.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        ((AppCompatActivity)this.Activity).SetSupportActionBar(mToolbar);
        ((AppCompatActivity)this.Activity).SupportActionBar.Title = "Progression Maker";

        //Button that when pressed returns to the login screen
        btnLogout = view.FindViewById<Button>(Resource.Id.btnLogout);
        btnLogout.Click += (sender, EventArts) =>
        {
            //Drops back into main activity
            var intent = new Intent(Activity, typeof(MainActivity));
            StartActivity(intent);
        };

        //Calls the to play the chords selected by the user
        btnPlay = view.FindViewById<Button>(Resource.Id.PlayChords);
        btnPlay.Click += (sender, EventArgs) => PlayChords(sender, EventArgs);

        //Calls this to generate random chords from the selected key
        btnRandomise = view.FindViewById<Button>(Resource.Id.btnRandomise);
        btnRandomise.Click += (sender, EventArgs) => randomiseChords(sender, EventArgs);

        //Spinners to store tonality and key signatures
        key_Spinner = view.FindViewById<Spinner>(Resource.Id.key_Spinner);
        tonality_Spinner = view.FindViewById<Spinner>(Resource.Id.tonality_Spinner);

        //Spinners that allow the user to select the individual chord they want to use
        chord1_spinner = view.FindViewById<Spinner>(Resource.Id.chord1);
        chord2_spinner = view.FindViewById<Spinner>(Resource.Id.chord2);
        chord3_spinner = view.FindViewById<Spinner>(Resource.Id.chord3);
        chord4_spinner = view.FindViewById<Spinner>(Resource.Id.chord4);

        chord1_spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(generateChordOne);
        chord2_spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(generateChordTwo);
        chord3_spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(generateChordThree);
        chord4_spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(generateChordFour);

        //Calls procedure that changes the spinners content based on the selection of another
        tonality_Spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(tonalitySpinner_ItemSelected);
        key_Spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(keySpinner_ItemSelected);

        //Brings up the save dialog
        btnSave = view.FindViewById<ImageButton>(Resource.Id.SaveButton);
        btnSave.Click += (sender, EventArgs) =>
        {
            //Ensures thet the user is unable to access save function if they have nothing to save
            if (tonality_Spinner.SelectedItemPosition == 0)
            {
                Toast.MakeText(this.Activity, "Nothing to save!", ToastLength.Long).Show();
            }
            else if (UserID == "-1" && tonality_Spinner.SelectedItemPosition != 0)
            {
                Toast.MakeText(this.Activity, "You cannot save progressions as a guest!", ToastLength.Long).Show();
            }
            else if (UserID == "-1" && tonality_Spinner.SelectedItemPosition == 0)
            {
                Toast.MakeText(this.Activity, "You cannot save progressions as a guest! And there's nothing to save!", ToastLength.Long).Show();
            }
            else 
            {
                //Gets the contents of the selected items of the spinners as strings
                string chord1 = chord1_spinner.SelectedItem.ToString();
                string chord2 = chord2_spinner.SelectedItem.ToString();
                string chord3 = chord3_spinner.SelectedItem.ToString();
                string chord4 = chord4_spinner.SelectedItem.ToString();
                string tonality = tonality_Spinner.SelectedItem.ToString();
                string keysig = key_Spinner.SelectedItem.ToString();

                FragmentTransaction transaction = FragmentManager.BeginTransaction();
                DialogSave Save_Dialog = new DialogSave(chord1, chord2, chord3, chord4, tonality, keysig, UserID);
                Save_Dialog.Show(transaction, "dialog fragment");
            }

        };

        btnLoad = view.FindViewById<ImageButton>(Resource.Id.LoadButton);
        btnLoad.Click += (sender, EventArgs) =>
        {
            if (UserID == "-1")
            {
                Toast.MakeText(this.Activity, "Can't load as a guest!", ToastLength.Long).Show();
            }
            else
            {
                btnLoad_Click(sender, EventArgs);
            }
        };


        //Brings up the dialog fragment for the help tab
        btnHelp = view.FindViewById<ImageButton>(Resource.Id.HelpButton);
        btnHelp.Click += (sender, EventArgs) =>
        {
            //Pull up dialog
            FragmentTransaction transaction = FragmentManager.BeginTransaction();
            DialogHelp Help_Dialog = new DialogHelp();
            Help_Dialog.Show(transaction, "dialog fragment");
        };

        if (UserID == "-1")
        {
            btnLogout.Text = "Sign In";
        }

        return view;
    }

    public void OnCreateOptionsMenu(IMenu menu, MenuInflater inflater)
    {
        inflater.Inflate(Resource.Menu.toolbar_menu, menu);
        base.OnCreateOptionsMenu(menu, inflater);
    }

    public override bool OnOptionsItemSelected(IMenuItem item)
    {
        string textToShow;
        if (item.ItemId == Resource.Id.menu_info)
            textToShow = "Learn more about us affa";
        else
            textToShow = "overflooow";
        return base.OnOptionsItemSelected(item);
    }

Layout for fragment:片段布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id = "@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight = "?android:attr/actionBarSize"
        android:background = "?android:attr/colorPrimary"
        android:elevation = "4dp"
        android:theme = "@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows = "true"/>
</LinearLayout>

Toolbar layout:工具栏布局:

<?xml version="1.0" encoding="utf-8" ?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app ="http://schemas.android.com/apk/res-auto">
  <item android:id="@+id/menu_info"
        android:icon ="@drawable/ic_help_icon"
        app:showAsAction ="ifRoom"
        android:title ="Info"/>
  <item android:id ="@+id/menu_overflow"
        app:showAsAction ="never"
        android:title="Overflow"/>
</menu>

Style page:样式页面:

<?xml version="1.0" encoding = "utf-8"?>
<resources>
  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
  </style>

</resources>

I have already made my main activity take from 'AppCompatActivity'.我已经让我的主要活动来自'AppCompatActivity'。 I also already have reference my theme in the Activity.我也已经在活动中引用了我的主题。 I have also included 'SetHasOptionsMenu(true)' in the OnCreate.我还在 OnCreate 中包含了“SetHasOptionsMenu(true)”。 The problem is it sets the title just fine but it doesn't seem to be calling the OnCreateOptionsMenu.问题是它设置的标题很好,但它似乎没有调用 OnCreateOptionsMenu。 Any ideas?有任何想法吗?

Make sure to include this your main activity to allow your fragment to create the toolbar:确保将此包含在您的主要活动中,以允许您的片段创建工具栏:

 public override bool OnCreateOptionsMenu(IMenu menu)
    {
        MenuInflater.Inflate(Resource.Menu.toolbar_menu, menu);
        return base.OnCreateOptionsMenu(menu);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM