简体   繁体   English

如何自定义 TabbedPage 中栏的大小? [XAMARIN.FORMS]

[英]How to customize the size of the bar in a TabbedPage? [XAMARIN.FORMS]

I have a Tabbed Page, I have it already accommodated, but I feel that it is very small and I need the lower bar to be higher我有一个标签页,我已经容纳了它,但我觉得它很小,我需要较低的栏更高

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            x:Class="Mobile.View.Principal"
            xmlns:local="clr-namespace:Mobile.View"
            xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
            BackgroundColor="White"
            BarTextColor="{StaticResource Gris}"
            BarBackgroundColor="white">
    
    
    <local:Inicio Title="Inicio" Icon="home_icon.png" HeightRequest="30" WidthRequest="30"/>
    <local:Consultas Title="Consultas" Icon="search_icon.png"/>
    <local:Utilidades Title="Utilidades" Icon="settings_icon.png"/>
    <local:Perfil Title="Perfil" Icon="favorites_icon.png"/>
    
</TabbedPage>

and this is the code behind:这是背后的代码:

public partial class Principal : Xamarin.Forms.TabbedPage
{
    public Principal()
    {
        InitializeComponent();
        On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
        On<Xamarin.Forms.PlatformConfiguration.Android>().SetBarSelectedItemColor(Color.Black);
        On<Xamarin.Forms.PlatformConfiguration.Android>().SetBarItemColor(Color.LightGray);
        //On<Xamarin.Forms.PlatformConfiguration.Android>().DisableSwipePaging(); //Disable sliding the pages with your finger.
        NavigationPage.SetHasNavigationBar(this, false);  // Hide nav bar
    }

    async void OnPerfilAsync(object sender, EventArgs e)
    {
        await Navigation.PushAsync(new Perfil());
    }
}

and that's how the app looks:这就是应用程序的外观:

在此处输入图片说明

HOW IS THE NAME OF THE BAR HEIGHT PROPERTY?酒吧高度属性的名称如何?

I need some help!!!我需要帮助!!!

On which platform do you want to change it?您想在哪个平台上更改它? You will likely need to implement a custom renderer for each platform that you want to make this change.您可能需要为要进行此更改的每个平台实现自定义渲染器

For iOS, you can add a new general class file to the iOS project named MyTabbedPageRenderer .对于 iOS,您可以向名为MyTabbedPageRenderer的 iOS 项目添加一个新的通用类文件。 Then in the file, add this line above the namespace declaration:然后在文件中,在namespace声明上方添加这一行:

[assembly: Xamarin.Forms.ExportRenderer(typeof(Xamarin.Forms.TabbedPage), typeof(YourNamespace.MyTabbedPageRenderer))]

where YourNamespace is the namespace that MyTabbedPageRenderer is in, ie the namespace just below where you have this attribute其中YourNamespaceMyTabbedPageRenderer所在的名称空间,即您拥有此属性的正下方的名称空间

Now make the class inherit from Xamarin.Forms.Platform.iOS.TabbedRenderer , eg:现在使该类从Xamarin.Forms.Platform.iOS.TabbedRenderer继承,例如:

public class MyTabbedPageRenderer : Xamarin.Forms.Platform.iOS.TabbedRenderer

Then add the following override method to the class:然后将以下覆盖方法添加到类中:

public override void ViewWillLayoutSubviews()
    {
        base.ViewWillLayoutSubviews();
        var newHeight = TabBar.Frame.Height + 50;
        CoreGraphics.CGRect tabFrame = TabBar.Frame; //self.TabBar is IBOutlet of your TabBar
        tabFrame.Height = newHeight;
        tabFrame.Y = View.Frame.Size.Height - newHeight;
        TabBar.Frame = tabFrame;
    }

The above will increase the Tabbar height on iOS by 50 pixels from the default.以上将在 iOS 上将 Tabbar 高度从默认值增加 50 像素。 Change the 50 to whatever value you like.50更改为您喜欢的任何值。

And for Android, you actually do not need to make a custom renderer since there is an Android layout that you can set a minHeight for the TabLayout.对于 Android,您实际上不需要制作自定义渲染器,因为有一个 Android 布局,您可以为 TabLayout 设置minHeight To do this, open the Tabbar.axml file in the resources/layout folder in your Android project.为此,请打开 Android 项目中 resources/layout 文件夹中的 Tabbar.axml 文件。 Then add an android:minHeight attribute to the android.support.design.widget.TabLayout element:然后向android.support.design.widget.TabLayout元素添加一个android:minHeight属性:

<android.support.design.widget.TabLayout 
    ...
    android:minHeight="300dp"
    />

This sets the height to a fixed 300 pixels.这将高度设置为固定的 300 像素。 Set it to whatever you want.将其设置为您想要的任何内容。

Alternately you can set the height in the MainActivity.OnCreate method.或者,您可以在MainActivity.OnCreate方法中设置高度。 The below is starting from a template Xam.Forms TabbedPage project.下面是从模板 Xam.Forms TabbedPage项目开始的。 Put the following after the LoadApplication(new App()) call:LoadApplication(new App())调用之后放置以下内容:

Android.Support.Design.Widget.TabLayout tabBar = FindViewById<Android.Support.Design.Widget.TabLayout>(Resource.Id.sliding_tabs);
tabBar.SetMinimumHeight(300);

Change the Resource.Id to whatever the android:id is for the TabLayout in the Tabbar.axml fileResource.Id更改为 Tabbar.axml 文件中TabLayoutandroid:id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM