2017-05-22 128 views
0

我有一個由Entry和Button組成的簡單佈局。目標是將Button放置在底部,並將Entry放置在剩餘空間的中心。一切工作在開始。這裏是佈局和截圖。當軟鍵盤顯示/隱藏時,ScrollView中的佈局更新不正確

<?xml version="1.0" encoding="utf-8"?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      NavigationPage.HasNavigationBar="False" 
      x:Class="ParentAdda.Pages.Test"> 
    <ScrollView x:Name="Qq" Orientation="Vertical" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"> 
     <StackLayout x:Name="Ww" Orientation="Vertical" VerticalOptions="FillAndExpand" 
        HorizontalOptions="FillAndExpand"> 
      <BoxView VerticalOptions="FillAndExpand" HeightRequest="0" BackgroundColor="Aquamarine" /> 
      <Entry 
       FontSize="Medium" 
       Placeholder="+111111111" 
       HorizontalOptions="FillAndExpand" 
       Keyboard="Telephone" /> 
      <BoxView VerticalOptions="FillAndExpand" HeightRequest="0" BackgroundColor="Coral" /> 
      <Button Text="Update" Clicked="Button_OnClicked" 
        HorizontalOptions="Fill" 
        BorderRadius="20" 
        BackgroundColor="Lime" 
        TextColor="White" 
        FontSize="Large" 
        FontAttributes="Bold" /> 
     </StackLayout> 
    </ScrollView> 
</ContentPage> 

enter image description here

我也設置WindowSoftInputMode在Android項目來調整(代碼中,考慮到Xamarin.Forms的bug當標籤被重置使用FormsAppCompatActivity時清單當設置爲潘)

protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     Xamarin.Forms.Forms.Init(this, bundle); 
     LoadApplication(new App()); 

     //https://bugzilla.xamarin.com/show_bug.cgi?id=39765 
     App.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize); 
     //https://bugzilla.xamarin.com/show_bug.cgi?id=39765 
     //Remove the status bar underlay in API 21+ 
     if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) 
     { 
      Window.DecorView.SystemUiVisibility = 0; 
      var statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); 
      statusBarHeightInfo?.SetValue(this, 0); 
      Window.SetStatusBarColor(Android.Graphics.Color.Black); 
     } 
    } 

入境時獲得焦點,該頁面未調整(雖然我可以滾動至最底部時,軟鍵盤是可見的)

enter image description here

當軟鍵盤隱藏的內容被錯誤地調整大小,並佔據屏幕的一部分。 enter image description here

它看起來像佈局過程是基於邊界執行前的軟鍵盤變得可見/不可見。然而,這一切似乎Bounds特性(頁,滾動型和StackLayout)以及滾動型的ContentSize酒店有正確的數值(我跟蹤他們在按鈕按下的處理程序)。我試圖在相同的按鈕點擊處理程序中的不同元素上調用ForceLayout(),但沒有運氣。

有誰知道如何解決這個問題?

回答

0

我不認爲這是怎樣的VerticalOptionsFillAndExpand設計,它是當沿着從父佈局Y軸可用多餘空間 元素的佈局。如果佈局的多個子項設置爲擴展,則額外空間按比例分配。

由於您沒有任何固定的尺寸,當鍵盤消失時,佈局會調整大小,我猜測FillAndExpand會首先測量軸上多餘的空間,然後爲孩子分配空間。

一個非常簡單的解決方法來解決你的問題是給一個固定大小的StackLayout,但你說

我們的目標是將底部按鈕和輸入中的剩餘空間的中心。

如果你想在這裏維持原有的規模,你可以將高度設置爲當它出現StackLayout,意味着覆蓋在代碼中OnAppearing()方法落後,例如:

protected override void OnAppearing() 
{ 
    base.OnAppearing(); 
    Ww.HeightRequest = Ww.Height; 
}