键盘向上滚动并隐藏Android上的工具栏和状态栏

时间:2017-04-03 10:13:39

标签: xamarin xamarin.android xamarin.forms

我希望在显示软键盘的同时保持状态栏和工具栏在顶部可见。我尝试了很多方法,但做不到。试过windowSoftInputMode并设置/清除windows的标志,但都失败了。 有人可以帮帮我吗? Please click here to see gif file!

1 个答案:

答案 0 :(得分:5)

这是一个Xamarin错误。请参阅here

有一些工作方法。您必须在MainActivity中执行以下操作:

protected override void OnCreate(Bundle bundle)
{
    ToolbarResource = Resource.Layout.toolbar;
    TabLayoutResource = Resource.Layout.tabs;

    base.OnCreate(bundle);

    //Remove the status bar underlay in API 21+
    if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
    {
        Window.DecorView.SystemUiVisibility = 0;
        var statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        statusBarHeightInfo.SetValue(this, 0);
        Window.SetStatusBarColor(new Android.Graphics.Color(18, 52, 86, 255));
    }


    global::Xamarin.Forms.Forms.Init(this, bundle);
    LoadApplication(new App());

    App.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
}

如果您使用FormsApplicationActivity而不是FormsAppCompatActivity,则必须删除以下行:

var statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
statusBarHeightInfo.SetValue(this, 0);

您可以通过Jimmy Garrido here查看更多详细信息。

但要隐藏ActionBar,如果在MainActivity中使用以下代码,那么只有以上解决方案无效

Xamarin.Forms.Forms.SetTitleBarVisibility(Xamarin.Forms.AndroidTitleBarVisibility.Never);

解决问题,对我有用的是删除上面的这一行并创建一个特定的主题,以使ActionBar消失。

在Resources / values / Styles.xml下,我创建了一个新主题(如果已有主题,可以更改主题):

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarSize">0dp</item>
  </style>
</resources>

不要忘记在MainActivity中更改主题,例如:

[Activity(WindowSoftInputMode = SoftInput.AdjustPan, Label = "Test", Icon = "@drawable/icon", Theme = "@style/MyTheme",  MainLauncher = true, NoHistory = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

我希望这可以帮到你。