Xamarin Forms 2.0 AppCompat android 键盘模式

Posted

技术标签:

【中文标题】Xamarin Forms 2.0 AppCompat android 键盘模式【英文标题】:Xamarin Forms 2.0 AppCompat android keyboard mode 【发布时间】:2016-03-04 23:18:23 【问题描述】:

Xamarin 我更新到版本 4、Forms 和 2.0 版本。在 android 上,我使用 AppCompat。

我遇到了问题。以前,Android 键盘会导致调整视图大小。现在这不会发生。键盘出现在顶视图上。以及想要隐藏的元素。

我试过了:

[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:https://blog.xamarin.com/material-design-for-your-xamarin-forms-android-apps/

谢谢。

【问题讨论】:

【参考方案1】:

我已经解决了这个问题。反编译类 “Forms AppCompatActivity”并观察了 OnCreate 方法的工作原理。

结果,我翻出了如下代码:

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

        
    

添加“Window.SetSoftInputMode(SoftInput.AdjustResize);”很重要调用后base.OnCreate(bundle);

【讨论】:

如果把这段代码放到 Xamarin 中就好了。

以上是关于Xamarin Forms 2.0 AppCompat android 键盘模式的主要内容,如果未能解决你的问题,请参考以下文章

无法在 Rider 中使用 .NET Standard 2.0 Lib 加载 Xamarin Forms 项目

退出应用程序时在 Android 上的 Xamarin.Forms 中获取 NullReferenceException

Xamarin Forms UWP - 无法调试共享代码

带有 Realm 3.4.0 的 .Net Standard 2.0“无法解析引用”Xamarin 表单

Xamarin.Forms:如何在 Xamarin.Forms 跨平台项目中开发具有蓝牙连接的应用程序?

Xamarin.Forms:Forms.Context 已过时