Xamarin Forms 2.0 AppCompat android keyboard mode

我与影子孤独终老i 提交于 2019-12-21 22:20:03

问题


Xamarin I updated to version 4, Forms and 2.0 versions. On Android, I use AppCompat.

I had a problem. Previously, the Android keyboard caused resize view. Now this does not happen. The keyboard appears on the top view. And the desired Elements to be hiding.

I've tried:

[Activity(WindowSoftInputMode = SoftInput.AdjustResize, Label = "Title", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

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


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

            LoadApplication(new App());
            Window.DecorView.SetFitsSystemWindows(true);
        }
    }

Daylight AppCompat has been made on this lesson: https://blog.xamarin.com/material-design-for-your-xamarin-forms-android-apps/

Thank you.


回答1:


I have solved the problem. Decompile class "Forms AppCompatActivity" and watched how the method works OnCreate.

As a result, I turned out the following code:

[Activity(Label = "Title", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            global::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://stackoverflow.com/users/341631/joseph-johnson) for publishing the original Android solution on stackoverflow.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;
            }

        }
    }

It is important to add "Window.SetSoftInputMode(SoftInput.AdjustResize);" After calling base.OnCreate(bundle);



来源:https://stackoverflow.com/questions/34013548/xamarin-forms-2-0-appcompat-android-keyboard-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!