【问题标题】:Hiding Android Action Bar in Xamarin PCL project在 Xamarin PCL 项目中隐藏 Android 操作栏
【发布时间】:2023-03-12 19:52:02
【问题描述】:

如何在 Xamarin Forms 项目的 android 应用程序的所有页面中更改 ActionBar 的可见性。我在主要活动中使用了 ActionBar.Hide(),但它只在首页隐藏它。

namespace ParsellIT.Droid
{
    [Activity(Label = "ParsellIT", MainLauncher = true)]
    public class MainActivity : AndroidActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            Xamarin.Forms.Forms.Init(this, bundle);            
            SetPage(App.GetMainPage());
            ActionBar.Hide();    
        }
    }
}

我想在每个页面中隐藏它,但我无法访问 android 特定代码中的每个页面,因为它们是在 PCL 中构建的。有什么办法吗?

【问题讨论】:

    标签: mono android-actionbar xamarin xamarin.forms


    【解决方案1】:

    更好的选择是调用:

    global::Xamarin.Forms.Forms.SetTitleBarVisibility(Xamarin.Forms.AndroidTitleBarVisibility.Never)
    

    var page = App.GetMainPage();
    Xamarin.Forms.NavigationPage.SetHasNavigationBar(page, true)
    

    或在 xaml 中

    <ContentPage
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Class="Mobile.Views.NewsFeedSearchView"
        NavigationPage.HasNavigationBar="{Binding IsFullScreen}"/>
    

    【讨论】:

      【解决方案2】:

      您应该在应用中使用没有操作栏的主题。例如,您可以在应用程序中指定应用程序属性:

      [assembly: Application(Icon = "@drawable/Icon" Theme = "@android:Theme.Holo.Light.NoActionBar")]
      

      或者你可以在AndroidManifaset.xml中设置

      <application android:theme="@android:style/Theme.Holo.Light.NoActionBar" />
      

      UPD 当然主题设置必须在Android项目中进行,但在PCL中也会影响到每个页面。

      【讨论】:

      • 它没有帮助。它仍然显示 ActionBar
      • 另一种选择是在 Resources/values/Style.xml 中创建自己的主题(如果没有,则创建一个)就像那里 stackoverflow.com/a/14061826/1558147 并使用它作为答案显示“Theme = "@style/MyTheme"跨度>
      • 将此添加到我的清单实际上会使我的应用程序崩溃,并在 MainActivity 中的 SetPage 上出现 NullReferenceException
      • 对不起,为什么这被标记为答案?,该建议不能在 Xamarin PCL 项目中完成。
      • @AhmedAlejo 当然不能。你应该在 Android 项目中这样做
      猜你喜欢
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多