【问题标题】:Adjust resize does not work in API 22 with AppCompat on Xamarin.Forms调整调整大小在 Xamarin.Forms 上的 AppCompat 的 API 22 中不起作用
【发布时间】:2016-04-05 07:20:23
【问题描述】:

我在 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。关于这种行为背后的原因有什么想法吗?

【问题讨论】:

    标签: android xamarin android-appcompat xamarin-forms


    【解决方案1】:

    在 MainActivity 的 OnCreate 方法中,您应该将输入模式设置为 AdjustResize。然后,为了处理here 报告的android bug,您可以使用下面的实用程序类(最初由@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://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;
        }
    }
    

    【讨论】:

    • 嗯,这不是一个真正的解决方案,因为我想使用AdjustResize 而不是AdjustPan :)
    • 这很合理。进行了更多搜索,我认为您应该查看另一个 stackoverflow 问题,该问题描述了输入模式的错误以及如何修复它:stackoverflow.com/questions/34013548/…希望这会有所帮助!
    • 这实际上有很大帮助!它解决了这个问题。我不知道您是否可以,但是如果您将其发布为答案,我会将其标记为已接受:)
    • 不适用于某些设备。例如带有 Android 8.1 的 Galaxy Tab A
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多