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

时间:2017-03-18 15:42:22

标签: android xamarin xamarin.android navigationbar android-immersive

这个问题似乎经常被问到,但是查看各种答案(非Xamarin),不使用Xamarin ...

在我的活动中,我尝试了各种形式:

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;
  }
}

我也在活动OnCreate()中尝试过此操作。似乎没什么用。

我看过: 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:

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();
    }
  }
}

发现问题:

我的搜索片段中的此编辑框导致键盘显示,打破全屏模式,即使键盘消失,全屏也不会返回。

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

3 个答案:

答案 0 :(得分:2)

我建议@Grace Feng回答一点改进。如果我们将一个构造函数添加到侦听器的类

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;
        }

    }
}

我们可以在任何活动中使用它:

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

        int altezza_schermo = vista.Height;

        View decorView = Window.DecorView;

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

答案 1 :(得分:1)

  

我发现原因是我的SearchFragment中的EditText,它会占用焦点并调出键盘,这使得即使在键盘消失后也无法正常工作。

我认为此问题的解决方法是检测键盘是否打开,如果键盘已关闭,请再次将视图设置为全屏。

您可以创建一个侦听器来检测键盘是否已关闭。例如:

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;

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;
        }
    }
}

经过测试,键盘关闭时效果很好。

答案 2 :(得分:0)

Oncreate中的整个内容可以替换为:

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

此版本的Xamarin形式中大部分未知的东西