【问题标题】:On Maui.net, Why is Android is not automatically scroll ViewElement into view when keyboard appear?在 Maui.net 上,为什么当键盘出现时 Android 不会自动将 ViewElement 滚动到视图中?
【发布时间】:2023-02-04 04:09:10
【问题描述】:

在 .net-Maui 中,当我在 scrollView 中添加一个 ViewElement 并且键盘出现时,键盘隐藏了 ViewElement。 如果我删除 ScrollView 它工作正常,但我无法滚动内容。

在 Xamarin 上,Android 会自动将 ViewElement 滚动到视图中,那么为什么会发生这种变化?。

我必须在毛伊岛做一些不同的事情吗?

这是代码示例。

 public class TestView : ContentView
    {
        public TestView()
        {
            var layout = new AbsoluteLayout();
            var scrollView = new ScrollView { Content = layout, };
            Content = scrollView;
            var entry = new Entry();
            layout.Add(entry);
            AbsoluteLayout.SetLayoutBounds(entry, new Rect(20, 600, 200, 45));
        }
    }

如果我这样做,它会起作用,但这对我来说不是解决方案。

 public class TestView : ContentView
    {
        public TestView()
        {
            var layout = new AbsoluteLayout();
            Content = layout ;
            var entry = new Entry();
            layout.Add(entry);
            AbsoluteLayout.SetLayoutBounds(entry, new Rect(20, 600, 200, 45));
        }
    }

我希望 ViewElement 自动滚动到视图中。

【问题讨论】:

  • 在 Xamarin 上,您有相同的布局,包括 AbsoluteLayout 和 ScrollView?如果您删除 AbsoluteLayout,它是否适用于 ScrollView?
  • 是的,它在 ScrollView 中使用 AbsoluteLayout 在 Xamarin 上工作,我正在尝试将我的应用程序移植到 Maui insted of Xamarin。如果我在滚动视图中使用其他布局没有区别,我尝试了 RelativeLayout 和 StackLayout,这是同一个问题

标签: maui


【解决方案1】:

我得到了 Microsoft 的帮助来修复它。

答案在这里:https://learn.microsoft.com/en-us/answers/questions/1166483/why-is-android-is-not-automatically-scroll-viewele

来自微软: 要解决此问题,您可以将以下代码添加到 MainActivity 中,这将解决问题:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    Window.SetSoftInputMode(Android.Views.SoftInput.AdjustPan);
}

【讨论】:

  • 很高兴你得到帮助。为了确保这个答案在将来有用,即使链接断开,请从链接中复制足够的描述和代码,以便答案可以“独立存在”。这将在未来帮助其他人。谢谢你。
猜你喜欢
  • 1970-01-01
  • 2020-05-19
  • 2011-09-02
  • 2012-06-29
  • 2017-05-27
  • 1970-01-01
  • 1970-01-01
  • 2014-08-20
  • 2012-02-01
相关资源
最近更新 更多