4

这个问题似乎经常被问到,但是查看各种答案(非 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

活动.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"/>
4

3 回答 3

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));
于 2018-04-05T21:17:33.723 回答
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;
        }
    }
}

经过测试,关闭键盘时它可以正常工作。

于 2017-03-20T10:16:52.187 回答
0

Oncreate 中的所有内容都可以替换为:

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

此 Xamarin 表单版本中的大多数未知内容

于 2020-04-17T08:48:21.483 回答