Xamarin Forms Android键盘将整个页面向上移动

6
我在尝试编写一个 Xamarin.Forms 聊天应用程序。问题是:在 Android 上,一旦键盘弹出,整个页面(包括 ActionBar)都会向上移动。我已经通过使用一个 NuGet 包来调整页面大小,在 IOS 上解决了这个问题。
我已经在 Android 项目的 MainActivity.cs 中设置了 WindowSoftInputMode = Android.Views.SoftInput.AdjustResize,但它没有起作用。我还尝试通过重新计算屏幕大小手动调整页面大小,但我还没有找到在不同设备上精确计算键盘大小的解决方案。
有人之前遇到过同样的问题吗?是否有一个适用于所有支持平台的官方 Xamarin.Forms 解决方案?
这是目前我的聊天布局:
<ContentPage.Content>
<StackLayout Padding="0">
  <ScrollView>
    <ListView HasUnevenRows="true" x:Name="lvChat" >
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <StackLayout Orientation="Vertical" VerticalOptions="Fill" HorizontalOptions="Fill" Padding="5">


              <Label Text="{Binding Author, StringFormat='{0}: '}" TextColor="Navy" FontSize="14"  />
              <Label Text="{Binding Text}" TextColor="Black" FontSize="15" />
              <Label Text="{Binding Time}" TextColor="Black" FontSize="14"  />
              <!-- Format for time.. , StringFormat='{0:HH:mm}' -->

            </StackLayout>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </ScrollView>


  <StackLayout Orientation="Horizontal" Padding="0" Margin="5, 5, 5, 5">
    <Entry Keyboard="Chat" x:Name="tbxChatInput" HorizontalOptions="FillAndExpand" Placeholder="Send a Message..." />
    <Button x:Name="btnSendMsg" HorizontalOptions="End" Text="Send" Margin="5"/>

  </StackLayout>
</StackLayout>

提前感谢您!

2个回答

2

你可以把包含输入和发送按钮的stacklayout放在scrollview里面,这样当你点击输入框时,键盘就会弹出来,你可以看到你的输入内容,而且在iOS和Android上都能正常工作。试一下这个:

 <ContentPage.Content>
       <StackLayout Padding="0">
          <ScrollView>

           <StackLayout>

             <ListView HasUnevenRows="true" x:Name="lvChat" >
                <ListView.ItemTemplate>
                   <DataTemplate>
                      <ViewCell>
                        <StackLayout Orientation="Vertical" VerticalOptions="Fill" HorizontalOptions="Fill" Padding="5">


                        <Label Text="{Binding Author, StringFormat='{0}: '}" TextColor="Navy" FontSize="14"  />
                        <Label Text="{Binding Text}" TextColor="Black" FontSize="15" />
                        <Label Text="{Binding Time}" TextColor="Black" FontSize="14"  />
                  <!-- Format for time.. , StringFormat='{0:HH:mm}' -->

                </StackLayout>
              </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>

        <StackLayout Orientation="Horizontal" Padding="0" Margin="5, 5, 5, 5">
        <Entry Keyboard="Chat" x:Name="tbxChatInput" HorizontalOptions="FillAndExpand" Placeholder="Send a Message..." />
        <Button x:Name="btnSendMsg" HorizontalOptions="End" Text="Send" Margin="5"/>
       </StackLayout>

      </StackLayout>

      </ScrollView>
    </StackLayout>
    </ContentPage.Content>

0

例如,在您的Android清单中尝试设置windowSoftInputMode

<application ... >
    <activity
        android:windowSoftInputMode="adjustResize" ... >
        ...
    </activity>
    ...
</application>

我相信这应该和“我已经尝试在MainActivity.cs中设置WindowSoftInputMode = Android.Views.SoftInput.AdjustResize”的情况是一样的,Tobi已经尝试过了。 - Chucky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接