【发布时间】:2015-08-07 12:13:32
【问题描述】:
我正在尝试为不同的页面创建不同的操作栏。例如,一些页面会有一个标题和一个帮助按钮,而其他页面应该有一个设置按钮。
如何使用 Xamarin.Android 中的自定义导航渲染器来实现这一点?
自定义导航渲染器的示例代码:
[assembly: ExportRenderer(typeof(CustomNavigationPage), typeof(CustomNavigationPageRenderer))]
namespace MobileAppSamples.Droid.CustomRenderers
{
public class CustomNavigationPageRenderer : NavigationRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
{
base.OnElementChanged(e);
if (Element != null)
{
var actionBar = ((Activity)Context).ActionBar;
Android.Widget.LinearLayout layout = new Android.Widget.LinearLayout(Context);
layout.Orientation = Orientation.Horizontal;
layout.WeightSum = 100;
TextView title = new TextView(Context);
title.Text = "sample";
ViewGroup.LayoutParams textlp = new ViewGroup.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
layout.AddView(title, textlp);
actionBar.SetCustomView(layout, new ActionBar.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent));
actionBar.SetDisplayOptions(ActionBarDisplayOptions.ShowCustom, ActionBarDisplayOptions.ShowCustom | ActionBarDisplayOptions.ShowHome );
}
}
}
}
【问题讨论】:
标签: android xamarin android-actionbar custom-controls navigationbar