繁体   English   中英

当层次结构中存在NestedScrollView时,Android不会调整我的布局大小

[英]Android doesn't resize my layout when there is a NestedScrollView in the hierarchy

我刚刚在使用Android 7.1.1和Android支持库25.3.1的Nexus 9上观察到以下行为。

这是我的活动布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="false"
        android:orientation="vertical"
        android:paddingBottom="4dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <View
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="#fffaaa" />

        <View
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="#bbbaaa" />

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:background="#f00" />

    </LinearLayout>
</ScrollView>

这就是它在屏幕上的样子:

在此输入图像描述

当屏幕键盘显示时,系统会调整活动的布局大小,从而占用从屏幕顶部到键盘的空间。 请在下面的屏幕截图中考虑红色虚线:

在此输入图像描述

但是,当我用NestedScrollView替换ScrollView时,系统不会调整布局大小:

在此输入图像描述

现在红色虚线位于键盘下方。 通过将android:windowSoftInputMode="adjustResize"应用于活动可以轻松解决该问题:

在此输入图像描述

红色虚线现在位于键盘上方。 我的问题是:

  1. 为什么我会观察到这种行为?
  2. NestedScrollView什么问题?

Android 文档说:

当输入法出现在屏幕上时,它会减少应用程序UI可用的空间量。 系统会决定如何调整UI的可见部分,但可能无法正确完成。 为确保应用程序的最佳行为,您应指定系统在剩余空间中显示UI的方式。

这可能是引用的不稳定的一个实例。

在我看来,这不是NestedScrollViewScrollView的问题,但更多的是Activity如何根据键盘调整UI。 之间的唯一区别NestedScrollViewScrollViewNestedScrollView已成为既父母 孩子的能力 我认为这说明了为什么你应该按照上述建议说:

为确保应用程序的最佳行为,您应指定系统在剩余空间中显示UI的方式。

即始终使用android:windowSoftInputMode="adjustResize" ,而不仅仅是在需要时。

这里的问题不是嵌套滚动视图,问题是当键盘在Input方法的调用中出现/消失时,Android的视图自动调整。 我希望您能理解下面给出的链接中的问题:

https://developer.android.com/training/keyboard-input/visibility.html

为什么使用修复大小。

            <View
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="#fffaaa" />

尝试

           <View
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:minHeight="300dp"
           android:background="#fffaaa" />

在开始任何事情之前,只需使粗糙的纸张工作,你想要什么样的设计

试试这可能对你有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM