简体   繁体   English

如何在 xamarin android 中隐藏导航栏?

[英]How to hide the navigation bar in xamarin android?

This question seems to be asked quite often, but looking through the various answers (non Xamarin) non of them work with Xamarin ...这个问题似乎经常被问到,但是查看各种答案(非 Xamarin),其中没有一个与Xamarin一起工作......

In my activity I have tried various forms of doing:在我的活动中,我尝试了各种形式的做:

public override void OnWindowFocusChanged( bool hasFocus ) {
  base.OnWindowFocusChanged( hasFocus );

  if ( hasFocus ) {
    var uiOptions =
      SystemUiFlags.HideNavigation |
      SystemUiFlags.LayoutHideNavigation |
      SystemUiFlags.LayoutFullscreen |
      SystemUiFlags.Fullscreen |
      SystemUiFlags.LayoutStable |
      SystemUiFlags.ImmersiveSticky;

    Window.DecorView.SystemUiVisibility = (StatusBarVisibility) uiOptions;
  }
}

I have also tried this in the activity OnCreate() .我也在活动OnCreate()尝试过这个。 Nothing seems to work.似乎没有任何效果。

I have read:https://developer.android.com/training/system-ui/immersive.html https://cmsdk.com/android/hide-navigation-bar-android-xamarin.html http://codegur.com/28394281/in-xamarin-android-how-can-i-hide-the-navigation-bar我已阅读:https ://developer.android.com/training/system-ui/immersive.html https://cmsdk.com/android/hide-navigation-bar-android-xamarin.html http://codegur。 com/28394281/in-xamarin-android-how-can-i-hide-the-navigation-bar

Activity.cs:活动.cs:

using Android.App;
using Android.Graphics;
using Android.OS;
using Android.Support.V4.Content;
using Android.Views;
using Android.Widget;
using Calligraphy;
using Dictionary.Fragments;
using Dictionary.Logic.Translation;
using Newtonsoft.Json;

namespace Dictionary.Activities {

  [Activity( MainLauncher = true )]
  public class MainActivity: Activity {

    protected override void OnCreate( Bundle bundle ) {
      base.OnCreate( bundle );
      SetContentView( Resource.Layout.Main );
      SetToolbar();
      SetStatusBarColor();
      ShowSearchFragment();
    }

    public override void OnWindowFocusChanged( bool hasFocus ) {
      base.OnWindowFocusChanged( hasFocus );

      if ( hasFocus ) {
        var uiOptions =
          SystemUiFlags.HideNavigation |
          SystemUiFlags.LayoutHideNavigation |
          SystemUiFlags.LayoutFullscreen |
          SystemUiFlags.Fullscreen |
          SystemUiFlags.LayoutStable |
          SystemUiFlags.ImmersiveSticky;

        Window.DecorView.SystemUiVisibility = (StatusBarVisibility) uiOptions;
      }
    }

    public override bool OnCreateOptionsMenu( IMenu menu ) {
      MenuInflater.Inflate( Resource.Menu.Toolbar, menu );
      return base.OnCreateOptionsMenu( menu );
    }

    protected override void AttachBaseContext( Android.Content.Context @base ) {
      base.AttachBaseContext( CalligraphyContextWrapper.Wrap( @base ) );
    }

    private void SetStatusBarColor() {
      Window.AddFlags( WindowManagerFlags.DrawsSystemBarBackgrounds );
      Window.SetStatusBarColor( new Color( ContextCompat.GetColor( this, Resource.Color.PrimaryColorDark ) ) );
    }

    private void SetToolbar() {
      var toolbar = FindViewById<Toolbar>( Resource.Id.toolbar );
      SetActionBar( toolbar );
      ActionBar.Title = GetString( Resource.String.AppName );
    }

    private void ShowSearchFragment() {
      var searchFragment = new SearchFragment();
      searchFragment.TranslationSelected += OnTranslationSelected;
      var fragTrans = FragmentManager.BeginTransaction();
      fragTrans.Add( Resource.Id.fragHolder, searchFragment );
      fragTrans.Commit();
    }
  }
}

Issue found:发现问题:

This edit box in my Search Fragment is causing the keyboard to show, breaking the fullscreen mode, even when the keyboard is gone, full screen does not return.我的搜索片段中的这个编辑框导致键盘显示,打破了全屏模式,即使键盘不见了,全屏也不会返回。

<EditText android:id="@+id/txtSearch"
                    android:layout_width="match_parent"
                    android:layout_height="48dp"
                    android:background="#FFF"
                    android:gravity="center"
                    android:hint="@string/SearchHint"
                    android:imeOptions="flagNoExtractUi|actionDone"
                    android:inputType="textNoSuggestions"
                    android:singleLine="true"
                    android:textColorHint="#ABABAB"
                    android:textSize="18sp"
                    fontPath="Fonts/amiri-regular.ttf"
                    android:textCursorDrawable="@drawable/cursor"/>

I propose a little improvement to @Grace Feng answer.我建议对@Grace Feng 的回答进行一些改进。 If we add a constructor to the class of the listener如果我们在监听器的类中添加一个构造函数

public class OnGlobalLayoutListener : Java.Lang.Object, IOnGlobalLayoutListener
{

    private int mScreenheight;

    private View mDecorView;

    public OnGlobalLayoutListener(int mioScreenheight, View mioDecorView)
    {
        mScreenheight = mioScreenheight;
        mDecorView = mioDecorView;
    }

    public void OnGlobalLayout()
    {
        Rect r = new Rect();

        var keypadHeight = mScreenheight - r.Bottom;
        if (keypadHeight <= mScreenheight * 0.15)
        {
            var uiOptions =
                SystemUiFlags.HideNavigation |
                SystemUiFlags.LayoutFullscreen |
                SystemUiFlags.Fullscreen |
                SystemUiFlags.ImmersiveSticky;

            mDecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
        }

    }
}

we can use it in any activity:我们可以在任何活动中使用它:

        RelativeLayout vista = FindViewById<RelativeLayout>(Resource.Id.main);

        int altezza_schermo = vista.Height;

        View decorView = Window.DecorView;

        vista.ViewTreeObserver.AddOnGlobalLayoutListener(new OnGlobalLayoutListener(altezza_schermo, decorView));

I found the cause to be a EditText in my SearchFragment, which takes the focus and brings up the keyboard, which makes this not work, even after the keyboard is gone.我发现原因是我的 SearchFragment 中的 EditText,它获取焦点并调出键盘,这使得即使在键盘消失之后这也不起作用。

I think a workaround for this issue is to detect whether the keyboard is open or not, and if the keyboard is closed, set the view to full screen again.我认为此问题的解决方法是检测键盘是否打开,如果键盘关闭,则再次将视图设置为全屏。

You can create a listener to detect if the keyboard is closed.您可以创建一个侦听器来检测键盘是否关闭。 For example:例如:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);

    decorview = Window.DecorView;
    LinearLayout root = FindViewById<LinearLayout>(Resource.Id.rootView);
    screenheight = root.Height;
    root.ViewTreeObserver.AddOnGlobalLayoutListener(new mOnGlobalLayoutListener());
}

public static int screenheight;
public static View decorview;

And the mOnGlobalLayoutListener can be something like this: mOnGlobalLayoutListener可以是这样的:

public class mOnGlobalLayoutListener : Java.Lang.Object, IOnGlobalLayoutListener
{
    public void OnGlobalLayout()
    {
        Rect r = new Rect();
        var keypadHeight = MainActivity.screenheight - r.Bottom;
        if (keypadHeight <= MainActivity.screenheight * 0.15)
        {
            var uiOptions =
                SystemUiFlags.HideNavigation |
                SystemUiFlags.LayoutHideNavigation |
                SystemUiFlags.LayoutFullscreen |
                SystemUiFlags.Fullscreen |
                SystemUiFlags.LayoutStable |
                SystemUiFlags.ImmersiveSticky;

            MainActivity.decorview.SystemUiVisibility = (StatusBarVisibility)uiOptions;
        }
    }
}

Tested and it works fine when the keyboard is closed.经测试,当键盘关闭时它工作正常。

The whole thing in Oncreate can be replaced with: Oncreate 中的全部内容可以替换为:

decorview = Window.DecorView;
decorview.ViewTreeObserver.AddOnGlobalLayoutListener(new mOnGlobalLayoutListener());

Most of the stuff in unknown with this release of Xamarin forms此版本的 Xamarin 表单中的大多数未知内容

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

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