当层次结构中有 NestedScrollView 时,Android 不会调整我的布局

标签 android scrollview android-support-library android-nestedscrollview

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

这是我的 Activity 布局:

<?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>

这是它在屏幕上的样子:

enter image description here

当屏幕键盘出现时,系统会调整我的 Activity 布局的大小,以便它占据从屏幕顶部到键盘的空间。请考虑以下屏幕截图中的红色虚线:

enter image description here

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

enter image description here

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

enter image description here

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

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

最佳答案

Android docs说:

When the input method appears on the screen, it reduces the amount of space available for your app's UI. The system makes a decision as to how it should adjust the visible portion of your UI, but it might not get it right. To ensure the best behavior for your app, you should specify how you'd like the system to display your UI in the remaining space.

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

在我看来,这不是 NestedScrollViewScrollView 的问题,而是 Activity 如何根据键盘调整 UI 的问题. NestedScrollViewScrollView 之间的唯一区别是 NestedScrollView 能够既是 又是 child. 我认为这强调了为什么你应该遵循上面的建议:

To ensure the best behavior for your app, you should specify how you'd like the system to display your UI in the remaining space.

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

关于当层次结构中有 NestedScrollView 时,Android 不会调整我的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43455321/

相关文章:

java - 保存recyclerview列表的状态

java - Firebase onDataChanged 调用了很多次(没有更新),有时使用空值

ios - 在之后/之前显示当前页码

android - 如果在 ScrollVIew 中添加,居中对齐的 UI 组件无法正确向上/向下滚动

android - ActionBarSherlock 和 HoloEverywhere 库之间的冲突

android - 为什么会这样?我该如何解决?安卓 : UNEXPECTED TOP-LEVEL EXCEPTION:

android - javax.net.ssl.SSLException : Not trusted server certificate 异常

android - 从 fbsdk React Native 获取数据

iphone - 我在 UIScrollView 中有一个 UIView。如何引用父 UIScrollView?

android - 无法在带有android adt插件的eclipse中使用复选框