xamarin android 标签,安卓端Tabbedpage调整在底部位置和标签及取消Android API28 以下的点击特效—-xamarin.forms学习笔记(一)...

使用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特效,以下提供一种方法供参考

4f8f9c3a49e4831599dd12128a733205.png

在安卓项目中,新建一个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;

}

}

}

}

c733af638e17b4be88acdace28c02cbb.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值