我按照xamarin android指南here
中给出的说明实现了android工具栏使用示例代码:
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.mainMenu, menu);
return base.OnCreateOptionsMenu(menu);
}
和xml:
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_share"
local:showAsAction="ifRoom"
android:title="Share" />
<item
android:id="@+id/menu_settings"
local:showAsAction="ifRoom"
android:title="Settings" />
</menu>
现在我想将工具添加到工具栏中,这些工具将来自我的网页api,它们基本上是我们网页的链接,例如隐私政策等。但是我无法在menu.Add方法上找到任何这样做的例子。基本上我需要的是从我的json中修剪meny项目,返回:
[{
pageid:1,
pagename:"home",
pagehtmllink:"....."
},
{
pageid:2,
pagename:"About",
pagehtmllink:"....."
},
...........
]
如何从代码背后实现这一目标?
答案 0 :(得分:1)
IMenu
有Add
方法,您可以致电:
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.main_menu, menu);
menu.Add(0, 99, 0, "DB Copy to SDCard (Debug)");
return base. OnCreateOptionsMenu(menu);
}
有关参数,请查看Android文档:Menu.html#add
要捕获菜单选项,您可以查看IMenuItem.ItemId
值:
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case 99:
DoSomething();
return true;
default:
return false;
}
}
答案 1 :(得分:0)
您不需要创建标签页,并且在每个页面上创建webview
public partial class MainPage : TabbedPage
{
public MainPage()
{
InitializeComponent();
this.BarBackgroundColor = Color.Gray;
var homePage = new HomePage();
var accountPage = new AccountPage();
var ordersPage = new OrdersPage();
var promotionsPage = new PromotionsPage();
var searchPage = new SearchPage();
homePage.Icon = "imgs_main.png";
accountPage.Icon = "imgs_account.png";
ordersPage.Icon = "imgs_orders.png";
promotionsPage.Icon = "imgs_promotions.png";
searchPage.Icon = "imgs_search.png";
homePage.Title = "Home";
accountPage.Title = "Account";
ordersPage.Title = "Orders";
promotionsPage.Title = "Promotions";
searchPage.Title = "Search";
Children.Add(homePage);
Children.Add(accountPage);
Children.Add(ordersPage);
Children.Add(promotionsPage);
Children.Add(searchPage);
}