【问题标题】:Xamarin Forms 2.0 AppCompat android keyboard modeXamarin Forms 2.0 AppCompat android 键盘模式
【发布时间】: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/

谢谢。

【问题讨论】:

    标签: xamarin android-softkeyboard xamarin.forms android-appcompat


    【解决方案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://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;
                }
    
            }
        }
    

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

    【讨论】:

    • 如果把这段代码放到 Xamarin 中就好了。
    猜你喜欢
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 2018-09-15
    • 2016-12-15
    • 2017-07-22
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    相关资源
    最近更新 更多