使用tabbedpage时将安卓端导航放在底部,官网也有说明方法.总结:
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:aya"
x:Class="aya.MainPage"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom"
android:TabbedPage.IsSwipePagingEnabled="false"
android:TabbedPage.IsSmoothScrollEnabled="false" >
其中重点在这两句
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom"
要去掉底部切换tab时的shift mode特效,以下提供一种方法供参考
在安卓项目中,新建一个TabbedpageRenderer,重写OnElementChange方法:
using Android.Content;
using Android.Support.Design.Internal;
using Android.Views;
using aya.Droid.Renderer;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppCompat;
[assembly: ExportRenderer(typeof(TabbedPage), typeof(BottomNavTabPageRenderer))]
namespace aya.Droid.Renderer
{
public class BottomNavTabPageRenderer : TabbedPageRenderer
{
public BottomNavTabPageRenderer(Context context) : base(context){}
protected override void OnElementChanged(ElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (ViewGroup != null && ViewGroup.ChildCount > 0)
{
BottomNavigationMenuView bottomNavigationMenuView = FindChildOfType(ViewGroup);
if (bottomNavigationMenuView != null)
{
var shiftMode = bottomNavigationMenuView.Class.GetDeclaredField("mShiftingMode");
shiftMode.Accessible = true;
shiftMode.SetBoolean(bottomNavigationMenuView, false);
shiftMode.Accessible = false;
shiftMode.Dispose();
for (var i = 0; i < bottomNavigationMenuView.ChildCount; i++)
{
if (!(bottomNavigationMenuView.GetChildAt(i) is BottomNavigationItemView item)) continue;
item.SetShiftingMode(false);
item.SetChecked(item.ItemData.IsChecked);
}
if (bottomNavigationMenuView.ChildCount > 0) bottomNavigationMenuView.UpdateMenuView();
}
}
T FindChildOfType(ViewGroup viewGroup) where T : Android.Views.View
{
if (viewGroup == null || viewGroup.ChildCount == 0) return null;
for (var i = 0; i < viewGroup.ChildCount; i++)
{
var child = viewGroup.GetChildAt(i);
if (child is T typedChild) return typedChild;
if (!(child is ViewGroup)) continue;
var result = FindChildOfType(child as ViewGroup);
if (result != null) return result;
}
return null;
}
}
}
}