如何使用Xamarin表单自定义渲染器隐藏Android选项卡?

时间:2022-12-02 18:09:32

In Xamarin Forms, I need to write a custom TabbedPageRenderer to hide the Android tabbar. However, I don't know how to do this.

在Xamarin格式中,我需要编写一个自定义选项卡pagerenderer来隐藏Android选项卡。但是,我不知道怎么做。

[assembly: ExportRenderer(typeof(CTabbedPage), typeof(CTabbedPageRenderer))]
namespace App15.Droid
{
    public class CTabbedPageRenderer : TabbedPageRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                (this.Context as Activity).ActionBar.NavigationMode = ActionBarNavigationMode.Standard;
            }
        }
    }
}

This code throws an exception because ActionBar is set to null. I'm using AppCompat 23.3.0 and XF 2.3.2.118-pre1.

这段代码抛出一个异常,因为ActionBar被设置为null。我使用的是AppCompat 23.3.0和XF 2.3.1.118 -pre1。

EDIT: I'm thinking the reason ActionBar is null is Toolbar has replaced it, but I still don't know how to hide tabs. Also, I'm not interested in pushing pages modally.

编辑:我在想ActionBar是null的原因是工具栏已经取代了它,但是我仍然不知道如何隐藏选项卡。另外,我对以模式推送页面不感兴趣。

I also tried adding android:visibility="gone" to Tabbar.axml. This successfully hides the tabbar but the tabbar still occupies space.

我还尝试将android:visibility="gone"添加到Tabbar.axml。这成功地隐藏了选项卡,但是选项卡仍然占用空间。

2 个解决方案

#1


0  

This is a known bug in Xamarin: android:visibility="gone" in Tabbar.axml does not reclaim space (Status: CONFIRMED).

这是Xamarin中一个已知的bug: android:visibility="gone"。axml不回收空间(状态:已确认)。

As soon as it's fixed, using above approach seems to be a way to go.

只要它是固定的,使用上面的方法似乎是一种方法。

#2


0  

Here is the perfect solution:

这里有一个完美的解决方案:

  1. Add android:visibility="gone" to Rescouces > layout > Tabbar.axml
  2. 添加android:visibility=" to rescoues> layout > Tabbar.axml

eg:

例如:

<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/sliding_tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:tabIndicatorColor="@android:color/white" app:tabGravity="fill" app:tabMode="fixed" android:visibility="gone" />

< ?xml version = " 1.0 " encoding = " utf - 8 " ?mso - fareast - font - family:宋体;mso - bidi - font - family: " times new roman "; mso - bidi - theme - font:attr / colorPrimary @style / ThemeOverlay.AppCompat.Dark“android:主题=”。ActionBar" app:tabIndicatorColor="@android:color/white" app:tabGravity="fill" app:tabMode="fixed" android:visibility="gone" />

  1. MainActivity.cs
  2. MainActivity.cs

Comment line ToolbarResource = Resource.Layout.Toolbar;

工具栏资源= Resource.Layout.Toolbar . toolbar;

eg:

例如:

namespace BottomTab.Droid
{
    [Activity(Label = "BottomTab.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            //ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);

            LoadApplication(new App());
        }
    }
}
  1. Add NavigationPage.SetHasNavigationBar(this, false); to each Pages in TabbedPage.
  2. 添加NavigationPage。SetHasNavigationBar(假);到页签中的每一页。

eg:

例如:

public partial class MyPage : ContentPage
{
    public MyPage()
    {
        InitializeComponent();
        NavigationPage.SetHasNavigationBar(this, false);
    }

    private void OnGoToProfile(object sender, EventArgs e)
    {
        Navigation.PushAsync(new ProfilePage());
    }
}

#1


0  

This is a known bug in Xamarin: android:visibility="gone" in Tabbar.axml does not reclaim space (Status: CONFIRMED).

这是Xamarin中一个已知的bug: android:visibility="gone"。axml不回收空间(状态:已确认)。

As soon as it's fixed, using above approach seems to be a way to go.

只要它是固定的,使用上面的方法似乎是一种方法。

#2


0  

Here is the perfect solution:

这里有一个完美的解决方案:

  1. Add android:visibility="gone" to Rescouces > layout > Tabbar.axml
  2. 添加android:visibility=" to rescoues> layout > Tabbar.axml

eg:

例如:

<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/sliding_tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:tabIndicatorColor="@android:color/white" app:tabGravity="fill" app:tabMode="fixed" android:visibility="gone" />

< ?xml version = " 1.0 " encoding = " utf - 8 " ?mso - fareast - font - family:宋体;mso - bidi - font - family: " times new roman "; mso - bidi - theme - font:attr / colorPrimary @style / ThemeOverlay.AppCompat.Dark“android:主题=”。ActionBar" app:tabIndicatorColor="@android:color/white" app:tabGravity="fill" app:tabMode="fixed" android:visibility="gone" />

  1. MainActivity.cs
  2. MainActivity.cs

Comment line ToolbarResource = Resource.Layout.Toolbar;

工具栏资源= Resource.Layout.Toolbar . toolbar;

eg:

例如:

namespace BottomTab.Droid
{
    [Activity(Label = "BottomTab.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            //ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);

            LoadApplication(new App());
        }
    }
}
  1. Add NavigationPage.SetHasNavigationBar(this, false); to each Pages in TabbedPage.
  2. 添加NavigationPage。SetHasNavigationBar(假);到页签中的每一页。

eg:

例如:

public partial class MyPage : ContentPage
{
    public MyPage()
    {
        InitializeComponent();
        NavigationPage.SetHasNavigationBar(this, false);
    }

    private void OnGoToProfile(object sender, EventArgs e)
    {
        Navigation.PushAsync(new ProfilePage());
    }
}