英文:
How do I delete the awkward empty space under icon in TabBar in .NET MAUI?
问题
我在AppShell
中使用TabBar
来创建一个选项卡。就像下面的代码一样,非常简单,但问题是我只想放置图标,并且图标应该自动垂直居中,但实际上,在图标下面有一个尴尬的空间。
<TabBar>
<Tab Icon="home.svg">
<ShellContent ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage"/>
</Tab>
<Tab Icon="plus.svg">
<ShellContent ContentTemplate="{DataTemplate local:AddDareTextPage}" Route="AddDareTextPage"/>
</Tab>
<Tab Icon="profile.svg">
<ShellContent ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage"/>
</Tab>
</TabBar>
如您在上面的图片中所见,图标并不垂直居中,而是稍微向上偏移了一点。我不知道是什么原因导致了这个问题,因为在代码中没有设置图标的位置,但后来我意识到这是为Title
留下的空间。
当我在代码中添加了<Tab Icon="home.svg" Title="MAIN">
时,一个图标下面就有了一个标题。如下图所示,不必要的空间消失了。
是否有办法只放置图标并垂直居中它们?我不想在TabBar
中放置任何Title
,但我找不到这样做的方法。
英文:
I'm using TabBar
in AppShell
to create a tab. Like the code below, it is very simple but the problem is that I just want to put the icon and the icon should automatically be vertically centered but instead, there is an awkward space under the icons.
<TabBar>
<Tab Icon="home.svg">
<ShellContent ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage"/>
</Tab>
<Tab Icon="plus.svg">
<ShellContent ContentTemplate="{DataTemplate local:AddDareTextPage}" Route="AddDareTextPage"/>
</Tab>
<Tab Icon="profile.svg">
<ShellContent ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage"/>
</Tab>
</TabBar>
As you can see with the above picture the icons are not vertically centered and it upwards a little bit. I didn't know what caused this because in the code, there is nothing that sets the location of the icon but then I realized it was the space for the Title
.
This is when I added <Tab Icon="home.svg" Title="MAIN">
in the code so there is a title under one icon. And as you can see in the image below, the unnecessary space is gone.
Is there a way to just put the icons and center them vertically? I don't want to put any Title
in TabBar
but I couldn't find a way to do this.
答案1
得分: 3
我们可以在.NET MAUI中使用使用自定义渲染器来移除额外的空间并使图标垂直居中。
目前,标题具有固定高度,因此在图标下方有空白空间。我们需要使用使用自定义渲染器来设置bottomView.LabelVisibilityMode = LabelVisibilityMode.LabelVisibilityUnlabeled;
以移除标题占用的额外空间并使图标垂直居中。
以下是详细的步骤:
- 在应用程序的
Platforms/Android/
目录中创建一个自定义的ShellRenderer类:
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Controls.Platform.Compatibility;
namespace MauiApp2
{
class CustomShellHandler : ShellRenderer
{
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new Platforms.Android.CustomShellBottomNavViewAppearanceTracker(this, shellItem.CurrentItem);
}
}
}
- 要自定义BottomNavView,请在同一目录中创建一个新的类CustomBottomNavViewAppearanceTracker:
using Android.Graphics.Drawables;
using Android.Views;
using Google.Android.Material.BottomNavigation;
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Controls.Platform.Compatibility;
using Microsoft.Maui.Platform;
namespace MauiApp2.Platforms.Android
{
class CustomShellBottomNavViewAppearanceTracker : ShellBottomNavViewAppearanceTracker
{
private readonly IShellContext shellContext;
public CustomShellBottomNavViewAppearanceTracker(IShellContext shellContext, ShellItem shellItem) : base(shellContext, shellItem)
{
this.shellContext = shellContext;
}
public override void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
base.SetAppearance(bottomView, appearance);
// 关键是像下面这样设置
bottomView.LabelVisibilityMode = LabelVisibilityMode.LabelVisibilityUnlabeled;
}
}
}
- 最后一步是在
MauiProgram.cs
中注册我们的处理程序,如下所示:
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureMauiHandlers(handlers =>
{
#if ANDROID
handlers.AddHandler(typeof(Shell), typeof(CustomShellHandler));
#endif
});
运行结果:
英文:
We can use the Using Custom Renderers in .NET MAUI to remove the extra space and get the icon vertically centered.
At the moment the title has a fix height so there is an empty space under the icon. We need use the Using Custom Renderers to set the bottomView.LabelVisibilityMode = LabelVisibilityMode.LabelVisibilityUnlabeled;
to remove the extra space occupied by title and get the icon vertically centered.
Here are the detailed steps listed below:
1.Create a custom ShellRenderer class in the Platforms/Android/
directory of your application:
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Controls.Platform.Compatibility;
namespace MauiApp2;
class CustomShellHandler : ShellRenderer
{
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new Platforms.Android.CustomShellBottomNavViewAppearanceTracker(this, shellItem.CurrentItem);
}
}
- To customize BottomNavView create a new class CustomBottomNavViewAppearanceTracker in the same directory:
using Android.Graphics.Drawables;
using Android.Views;
using Google.Android.Material.BottomNavigation;
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Controls.Platform.Compatibility;
using Microsoft.Maui.Platform;
namespace MauiApp2.Platforms.Android
{
class CustomShellBottomNavViewAppearanceTracker : ShellBottomNavViewAppearanceTracker
{
private readonly IShellContext shellContext;
public CustomShellBottomNavViewAppearanceTracker(IShellContext shellContext, ShellItem shellItem) : base(shellContext, shellItem)
{
this.shellContext = shellContext;
}
public override void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
base.SetAppearance(bottomView, appearance);
// the key is to set like below
bottomView.LabelVisibilityMode = LabelVisibilityMode.LabelVisibilityUnlabeled;
}
}
}
- The final step is registering our handlers in
MauiProgram.cs
like below:
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureMauiHandlers(handlers =>
{
#if ANDROID
handlers.AddHandler(typeof(Shell), typeof(CustomShellHandler));
#endif
})
Running output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论