c# - 如何在 Xamarin Android Fragment 中加载工具栏布局
问题描述
我正在尝试创建一个可以放置可点击图标的基本工具栏。这将位于起始活动页面的单独片段中。
片段代码:
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);
}
片段布局:
<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>
工具栏布局:
<?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>
样式页面:
<?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>
我已经将我的主要活动取自“AppCompatActivity”。我也已经在活动中引用了我的主题。我还在 OnCreate 中包含了“SetHasOptionsMenu(true)”。问题是它设置的标题很好,但它似乎没有调用 OnCreateOptionsMenu。有任何想法吗?
解决方案
确保将此包含在您的主要活动中,以允许您的片段创建工具栏:
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.toolbar_menu, menu);
return base.OnCreateOptionsMenu(menu);
}
推荐阅读
- laravel - Laravel Passport 自定义密码验证
- javascript - 用JS创建一个关闭按钮?
- ms-access - 无法保存为 .ACCDE 文件(无法使用
; 文件已在使用) - active-directory - 纯 ldap 查询来搜索设置了 DONT_EXPIRE_PASSWORD 标志的 useraccountcontrol
- c - C中点数组的括号位置之间的差异
- java - How to get a List from string from List of Map with Java 8 Stream?
- c - 无法从自定义 AXI 外设寄存器读取数据
- arm - 刚买了STM32F446,但STM32IDE没有达到我的预期
- javascript - Vanilla JS - 通过单击按钮隐藏和显示 div 的功能
- asp.net-core - 仅在一个横向扩展的 Azure 应用服务实例上运行 IHostedService