Adjust resize 在 API 22 和 AppCompat on Xamarin.Forms 中不起作用

Adjust resize does not work in API 22 with AppCompat on Xamarin.Forms

我在 Android 上有一个 Xamarin.Forms 项目,我想使用 "AdjustResize" 作为处理软键盘的默认方法。我通过在 OnCreate 中设置它来做到这一点:

[Activity(Label = "TestWhite.Droid", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : FormsAppCompatActivity
{
  protected override void OnCreate(Bundle bundle)
  {
    FormsAppCompatActivity.ToolbarResource = Resource.Layout.toolbar;
    FormsAppCompatActivity.TabLayoutResource = Resource.Layout.tabs;

    base.OnCreate(bundle);

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

    Window.SetSoftInputMode(SoftInput.AdjustResize);

    LoadApplication(new App());
  }
}

由于我已经更新到 AppCompatAdjustResize 不适用于 API 22,而它仍然适用于 API 19。

使用仅包含滚动视图和页面底部编辑器的一页的应用程序可以很容易地测试该问题:

public MainPage()
{
    InitializePage();
}

void InitializePage()
{
    var editor = new Editor
    {
        VerticalOptions = LayoutOptions.FillAndExpand,
        HorizontalOptions = LayoutOptions.FillAndExpand,
        HeightRequest = 200,
    };

    var tallLayout = new StackLayout
    {
        HorizontalOptions = LayoutOptions.FillAndExpand,
        Orientation = StackOrientation.Horizontal,
        HeightRequest = 450, 
        BackgroundColor = Color.Lime,
    };

    var layout = new StackLayout
    {
        Orientation = StackOrientation.Vertical,
        VerticalOptions = LayoutOptions.FillAndExpand,
        HorizontalOptions = LayoutOptions.FillAndExpand,
        Children = { tallLayout, editor },
    };

    var scrollView = new ScrollView
    {                
        VerticalOptions = LayoutOptions.FillAndExpand,
        HorizontalOptions = LayoutOptions.FillAndExpand, 
    };
    scrollView.Content = layout;

    Content = scrollView;
}

使用 API 19,当用户单击 Editor 时,视图会被推上去,以便用户可以看到他正在写的内容。相反,使用 API 22,键盘只出现在视图上,隐藏了 Editor。关于这种行为背后的原因有什么想法吗?

在 MainActivity 的 OnCreate 方法中,您应该将输入模式设置为 AdjustResize。然后,要处理 here 报告的 android 错误,您可以使用下面的实用程序 class(最初由 @user1658602 从 java 代码转换而来)。编码愉快!

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

    Xamarin.Forms.Forms.Init(this, bundle);

    Window.SetSoftInputMode(SoftInput.AdjustResize);

    AndroidBug5497WorkaroundForXamarinAndroid.assistActivity(this);

    ToolbarResource = Resource.Layout.toolbar;
    TabLayoutResource = Resource.Layout.tabs;

    LoadApplication(new App());

}

public class AndroidBug5497WorkaroundForXamarinAndroid
{

    // For more information, see https://code.google.com/p/android/issues/detail?id=5497
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

    // CREDIT TO Joseph Johnson (http://whosebug.com/users/341631/joseph-johnson) for publishing the original Android solution on whosebug.com

    public static void assistActivity(Activity activity)
    {
        new AndroidBug5497WorkaroundForXamarinAndroid(activity);
    }

    private Android.Views.View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    private AndroidBug5497WorkaroundForXamarinAndroid(Activity activity)
    {
        FrameLayout content = (FrameLayout)activity.FindViewById(Android.Resource.Id.Content);
        mChildOfContent = content.GetChildAt(0);
        ViewTreeObserver vto = mChildOfContent.ViewTreeObserver;
        vto.GlobalLayout += (object sender, EventArgs e) => {
            possiblyResizeChildOfContent();
        };
        frameLayoutParams = (FrameLayout.LayoutParams)mChildOfContent.LayoutParameters;
    }

    private void possiblyResizeChildOfContent()
    {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious)
        {
            int usableHeightSansKeyboard = mChildOfContent.RootView.Height;
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;

            frameLayoutParams.Height = usableHeightSansKeyboard - heightDifference;

            mChildOfContent.RequestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight()
    {
        Rect r = new Rect();
        mChildOfContent.GetWindowVisibleDisplayFrame(r);
        if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
        {
            return (r.Bottom - r.Top);
        }
        return r.Bottom;
    }
}