如何确定虚拟键盘高度?

时间:2016-09-04 08:54:45

标签: android ios xamarin xamarin.forms

我希望按钮始终位于虚拟键盘的正上方。为此,我需要知道键盘的高度。我怎么能在XF中做到这一点?

1 个答案:

答案 0 :(得分:0)

在主要活动中设置WindowSoftInputMode = SoftInput.AdjustResize并使用此代码:)

public Page1()
    {
        InitializeComponent();

        var al = new AbsoluteLayout
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand
        };

        var button = new BoxView {Color = Color.Red, VerticalOptions = LayoutOptions.FillAndExpand};

        var entry = new Entry {HorizontalOptions = LayoutOptions.Fill};

        al.Children.Add(button, new Rectangle(0, App.Dimensions.Height - 120, App.Dimensions.Width, 120));

        al.Children.Add(entry, new Rectangle(10, 50, 200, 60));

        Content = al;

        Content.SizeChanged += (sender, args) =>
        {
            button.Layout(new Rectangle(0, Content.Height - 120, App.Dimensions.Width, 120));
        };

    }

App.Dimensions在MainActivity中进行评估,如下所示:

        DisplayMetrics displayMetrics = Resources.DisplayMetrics;
        App.Dimensions.Height = displayMetrics.HeightPixels / displayMetrics.Density;
        App.Dimensions.Width = displayMetrics.WidthPixels / displayMetrics.Density;