[英]What decides the color of the tab bar in a Xamarin.Forms tabbed page?
[英]In Xamarin Forms 3.1, when using tabbed page with 4 tabs, how can I prevent “sliding” effect of tab bar on Android?
我有一個有四頁的應用程序,我希望它看起來類似於我的(非Xamarin)iOS應用程序,所以底部有工具欄。 這是我的MainPage.xaml文件:
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XaBLE1"
x:Class="XaBLE1.MainPage"
Title="Safe-T Sim" HeightRequest="768" WidthRequest="512"
BarBackgroundColor="#F1F1F1"
BarTextColor="Gray"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom"
android:TabbedPage.BarItemColor="#666666"
android:TabbedPage.BarSelectedItemColor="Black"
>
<NavigationPage Title="Test" Icon="ElectTest.png"
HasNavigationBar="False">
<x:Arguments>
<local:TestPage />
</x:Arguments>
</NavigationPage>
<NavigationPage Title="Review" Icon="Review.png"
HasNavigationBar="False">
<x:Arguments>
<local:ReviewPage />
</x:Arguments>
</NavigationPage>
<NavigationPage Title="Setup" Icon="Gear.png"
HasNavigationBar="False">
<x:Arguments>
<local:SetupPage />
</x:Arguments>
</NavigationPage>
<NavigationPage Title="Info" Icon="Info.png"
HasNavigationBar="False">
<x:Arguments>
<local:InfoPage />
</x:Arguments>
</NavigationPage>
</TabbedPage>
我不關心Oreo上的當前外觀,這是為了使選定的頁面選項卡更大並放置標題,將其他選項卡推到一邊並刪除頁面標題。
無論如何都要禁用此行為,並讓它只是4個選項卡。 請注意,如果有3個選項卡,則不會發生此行為 - 圖標和文本只有變暗和略微放大,但兩者都可見。
編輯:我嘗試了評論中提出的答案,但正如所指出的,我不確定這是在嘗試解決同樣的問題,並且在任何情況下都不會改變行為。
看起來你正在尋找這個(尚未實現)功能: [增強]實現底部導航欄Android(Github)的固定模式
我可以按照James Montemagno教程解決它:在Xamarin.Android中刪除BottomNavigationView的Icon Shifting並實現我自己的Tabbed頁面自定義渲染器:
using Android.Content;
using Android.Support.Design.Internal;
using Android.Views;
using FixedTabbedPage.Droid.CustomRenderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppCompat;
[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabbedPageRenderer))]
namespace FixedTabbedPage.Droid.CustomRenderers
{
public class CustomTabbedPageRenderer : TabbedPageRenderer
{
public CustomTabbedPageRenderer(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
{
base.OnElementChanged(e);
if (ViewGroup != null && ViewGroup.ChildCount > 0)
{
BottomNavigationMenuView bottomNavigationMenuView = FindChildOfType<BottomNavigationMenuView>(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++)
{
var item = bottomNavigationMenuView.GetChildAt(i) as BottomNavigationItemView;
if (item == null) continue;
item.SetShiftingMode(false);
item.SetChecked(item.ItemData.IsChecked);
}
if (bottomNavigationMenuView.ChildCount > 0) bottomNavigationMenuView.UpdateMenuView();
}
}
}
private T FindChildOfType<T>(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);
var typedChild = child as T;
if (typedChild != null) return typedChild;
if (!(child is ViewGroup)) continue;
var result = FindChildOfType<T>(child as ViewGroup);
if (result != null) return result;
}
return null;
}
}
}
您只需將此代碼添加到Android解決方案(重構命名空間),結果如下:
要禁用選項卡滑動,可以在TabbedPage類中使用PlatformConfiguration
public partial class MyTabbedPage : TabbedPage
{
public MainTabbedPage ()
{
InitializeComponent();
this.On<Xamarin.Forms.PlatformConfiguration.Android>().SetIsSwipePagingEnabled(false);
}
}
如果你沒有MyTabbedPage類,添加它比你的axml文件結構看起來像這樣
<?xml version="1.0" encoding="utf-8" ?>
<MyTabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XaBLE1.MainPage">
</MyTabbedPage>
看起來,Android 9之后還有一個更簡單的選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.