【问题标题】:How to hide Android tabs using Xamarin Forms custom renderer?如何使用 Xamarin Forms 自定义渲染器隐藏 Android 选项卡?
【发布时间】:2016-12-08 04:57:26
【问题描述】:

在 Xamarin Forms 中,我需要编写一个自定义 TabbedPageRenderer 来隐藏 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;
            }
        }
    }
}

此代码引发异常,因为 ActionBar 设置为 null。我正在使用 AppCompat 23.3.0 和 XF 2.3.2.118-pre1。

编辑:我认为ActionBar 为空的原因是Toolbar 已替换它,但我仍然不知道如何隐藏标签。另外,我对以模态方式推送页面不感兴趣。

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

【问题讨论】:

  • 如果您不想要选项卡,为什么要使用选项卡式布局?
  • 我不喜欢 Android 的标签位于顶部。我喜欢 iOS 的方法,所以我希望隐藏现有的标签栏并改用 Grid。
  • 只是一个建议:如果你要隐藏标签,Android 用户总是除了找到上面的标签(就像 WhatsApp 一样),那么也许你应该使用 Carousel View 来代替 android。跨度>
  • 在 XF 中,Carousel View 目前有很多问题。我知道 Material 中有底部导航栏,但它还不是官方的 Android 控件。我找到了一个 XF 端口,但它非常有问题。
  • 尝试为您的选项卡式布局使用负边距

标签: android xamarin tabs xamarin.android xamarin.forms


【解决方案1】:

这是known bug in Xamarin: android:visibility="gone" in Tabbar.axml does not reclaim space(状态:已确认)。

一旦修复,使用上述方法似乎是一种方法。

【讨论】:

    【解决方案2】:

    这是完美的解决方案:

    1. 将 android:visibility="gone" 添加到 Rescouces > 布局 > Tabbar.axml

    例如:

    <?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" />

    1. MainActivity.cs

    注释行 ToolbarResource = Resource.Layout.Toolbar;

    例如:

    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. 添加 NavigationPage.SetHasNavigationBar(this, false);到 TabbedPage 中的每个页面。

    例如:

    public partial class MyPage : ContentPage
    {
        public MyPage()
        {
            InitializeComponent();
            NavigationPage.SetHasNavigationBar(this, false);
        }
    
        private void OnGoToProfile(object sender, EventArgs e)
        {
            Navigation.PushAsync(new ProfilePage());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 1970-01-01
      • 2018-07-29
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多