gpt4 book ai didi

c# - 将位置设置为底部时不显示 TabbedPage 图标

转载 作者:行者123 更新时间:2023-11-29 23:20:32 27 4
gpt4 key购买 nike

我是 Xamarin 的初学者,正在尝试将 TabbedPage 用于我的应用程序。当我使用 TabbedPage 并设置图标时,它工作正常。

enter image description here

然后我使用下面的链接将 TabbedPage 位置设置为底部

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/platform/android/tabbedpage-toolbar-placement-color

但是,当我运行应用程序时,TabbedPage 图标不可见,甚至宽度对于一个 Tab 来说也太长了

enter image description here

下面是我的 XAML 代码:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="//xamarin.com/schemas/2014/forms"
xmlns:x="//schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:App5.Views"
x:Class="App5.Views.MainPage"
BarBackgroundColor="LightYellow"
BarTextColor="Black"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom"
android:TabbedPage.BarItemColor="Black"
android:TabbedPage.BarSelectedItemColor="Red">
<TabbedPage.Children>
<NavigationPage Title="Tab1" Icon="Tab1.png">
<x:Arguments>
<views:ItemsPage />
</x:Arguments>
</NavigationPage>
<NavigationPage Title="Tab2" Icon="Tab2.png">
<x:Arguments>
<views:AboutPage />
</x:Arguments>
</NavigationPage>
<NavigationPage Title="Tab3" Icon="Tab3.png">
<x:Arguments>
<views:AboutPage />
</x:Arguments>
</NavigationPage>
<NavigationPage Title="Tab4" Icon="Tab4.png">
<x:Arguments>
<views:AboutPage />
</x:Arguments>
</NavigationPage>
</TabbedPage.Children>

谁能帮我解决这个问题?

最佳答案

我认为选中时选项卡之间的空格可能有问题。选择选项卡时,您需要禁用缩放效果。

创建自定义标签阅读器以禁用 Android 的缩放效果。

BottomTabbedPageRenderer.cs

  [assembly: ExportRenderer(typeof(TabbedPage), typeof(BottomTabbedPageRenderer))]
namespace Droid.Renderer

{
public class BottomTabbedPageRenderer : TabbedPageRenderer
{
public BottomTabbedPageRenderer(Context context) : base(context)
{
}

protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);
try
{
var children = GetAllChildViews(ViewGroup);

if (children.SingleOrDefault(x => x is BottomNavigationView) is BottomNavigationView bottomNav)
{

try
{
if (!(bottomNav.GetChildAt(0) is BottomNavigationMenuView menuView))
{
System.Diagnostics.Debug.WriteLine("Unable to find BottomNavigationMenuView");
return;
}


var shiftMode = menuView.Class.GetDeclaredField("mShiftingMode");

shiftMode.Accessible = true;
shiftMode.SetBoolean(menuView, false);
shiftMode.Accessible = false;
shiftMode.Dispose();


for (int i = 0; i < menuView.ChildCount; i++)
{
if (!(menuView.GetChildAt(i) is BottomNavigationItemView item))
continue;

item.SetShiftingMode(false);
item.SetChecked(item.ItemData.IsChecked);

}

menuView.UpdateMenuView();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Unable to set shift mode: {ex}");
}

}

}
catch (Exception e)
{
Console.WriteLine($"Error setting ShiftMode: {e}");
}
}

private List<View> GetAllChildViews(View view)
{
if (!(view is ViewGroup group))
{
return new List<View> { view };
}

var result = new List<View>();

for (int i = 0; i < group.ChildCount; i++)
{
var child = group.GetChildAt(i);

var childList = new List<View> { child };
childList.AddRange(GetAllChildViews(child));

result.AddRange(childList);
}

return result.Distinct().ToList();
}
}
}

MyTabPage.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"
BarBackgroundColor="LightYellow"
BarTextColor="Black"
xmlns:views="clr-namespace:Demo"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom"
x:Class="Demo.MyTabPage">
<TabbedPage.Children>
<NavigationPage
Title="Tab1"
Icon="dashboard_selected.png">
<x:Arguments>
<views:MainPage />
</x:Arguments>
</NavigationPage>
<NavigationPage
Title="Tab2"
Icon="dashboard.png">
<x:Arguments>
<views:MainPage />
</x:Arguments>
</NavigationPage>
<NavigationPage
Title="Tab3"
Icon="error_alert.png">
<x:Arguments>
<views:MainPage />
</x:Arguments>
</NavigationPage>
<NavigationPage
Title="Tab4"
Icon="menu.png">
<x:Arguments>
<views:MainPage />
</x:Arguments>
</NavigationPage>
</TabbedPage.Children>
</TabbedPage>

我将所有图像资源都放在同名的 xhdpi、mdpi、xxhdpi、xxxhdpi 文件夹中,在我的例子中,它工作正常。

Demo

关于c# - 将位置设置为底部时不显示 TabbedPage 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54402001/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com