CoordinatorLayout的adjustResize不起作用

26

我的代码(和更多):我有一个协调布局,如下所示

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <include layout="@layout/top" /> <!-- A RelativeLayout -->

            <android.support.v7.widget.Toolbar
                android:id="@+id/MyToolbar"
                android:layout_width="match_parent"
                android:layout_height="64dp"
                app:layout_collapseMode="parallax">

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <!-- Main content here -->

    </FrameLayout>

</android.support.design.widget.CoordinatorLayout>
在框架布局中,我使用一个带有编辑文本的自定义视图,该文本位于底部。编辑文本和自定义视图都显示正常。
我在清单文件中定义了android:windowSoftInputMode="adjustResize|stateHidden"。
我的问题是:当点击编辑文本以弹出软键盘时,它会重叠在下方而不是调整FrameLayout大小。我无法让FrameLayout在软键盘弹出时调整大小。任何帮助将不胜感激。
我尝试过的事情:
1. 更新设计支持库至22.2.1 2. 在FrameLayout中添加android:layout_gravity="fill_vertical"属性。

我也遇到了完全相同的问题。你最终解决了吗? - Andrew
@Andrew. 我想不出任何方法来让它工作。最终,我不得不用相对布局替换协调布局,并进行自定义实现。 - hexhog
有人找到它的解决方案了吗? - Sagar Trehan
我有同样的问题。当选择EditText时,我的EditText和一些周围的元素需要被推上去。添加CoordinatorLayout会移除这个功能。 - filthy_wizard
嗨,我认为这不是CoordinatorLayout的问题。我尝试使用ObservableScrollView创建相同的UI。我遇到了与之前提到的相同的问题。问题出在android:fitsSystemWindows="true"或者window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)上。https://github.com/ksoichiro/Android-ObservableScrollView - Amrut Bidri
9个回答

18
这只是设计支持库中另一个错误,看起来他们没有计划立即修复它

确认过了,但可能不会立即修复。adjustResize有一些很好的用例,但与AppBarLayout和CollapsingToolbarLayout一起使用不是其中之一。

令人不安的是,在开发设计支持库时,没有人尝试将EditText添加到具有android:windowSoftInputMode =“adjustResize”CoordinatorLayout中。

我非常需要这个能够工作!很遗憾没有修复方法。 - Nick Isaacs

17

试一试


your_layout.xml

<RelativeLayout
    android:fitsSystemWindows="true" > <!-- Crucial! -->

    <android.support.design.widget.CoordinatorLayout>
        ...
        <android.support.v7.widget.RecyclerView />
        ...
    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

AndroidManifest.xml

<activity
    android:name=".YourActivity"
    android:windowSoftInputMode="adjustResize"> <!-- Crucial! -->
</activity>

解决了我的问题。 - Ali Behzadian Nejad
11
尽管这对大多数人可能有效,但无法让您拥有半透明的状态栏,并使工具栏内容显示在其后面。相反,状态栏将是不透明的,而内容则位于其下方。 - aProperFox
@aProperFox 我也注意到了!谢谢你提醒。 - Michael
@Michael 谢谢,BottomSheetFragment也可以用同样的方法吗?键盘覆盖了Editexts,有什么解决方案可用!!我使用了getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);以及你的解决方案,ScrollView正在滚动,但是字段没有出现在键盘顶部。 参考:https://dev59.com/IVkS5IYBdhLWcg3w6qXU - LOG_TAG
@aProperFox 可以确认。这个方法可以解决问题,但最终会导致你提到的那个问题。 - Lee Kang

10

我正在使用这个来解决 CoordinatorLayout 的 bug

在 BottomSheetFragment 的 onCreateDialog 中

KeyboardUtil(getActivity(), view);

或者

用于活动的使用

new KeyboardUtil(this, findViewById(R.id.fragment_container));
通过使用这个Util类
Java版本: https://github.com/mikepenz/MaterialDrawer/blob/aa9136fb4f5b3a80460fe5f47213985026d20c88/library/src/main/java/com/mikepenz/materialdrawer/util/KeyboardUtil.java Kotlin版本:https://gist.github.com/agustinsivoplas/6f80233699aebebd512542503ea32682 Credit:Mikepenz,Agustinsivoplas

1
这个救了我的一天!完美运行!Kotlin版本请参见https://gist.github.com/agustinsivoplas/6f80233699aebebd512542503ea32682 - AndroidRuntimeException
1
这个可以工作,但它会从视图底部移除大约60dp。 - AndroidDev123

7

我的布局

<android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.AppBarLayout>

        <android.support.design.widget.CollapsingToolbarLayout>

            <ImageView>

            <android.support.v7.widget.Toolbar>

        <android.support.design.widget.CollapsingToolbarLayout>

    <android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton>

<android.support.design.widget.CoordinatorLayout>

我曾遇到同样的问题。我发现,如果我在例如NestedScrollViewFrameLayout中使用某些内容来处理滚动本身,CoordinatorLayout不会根据你想要的方式进行adjustResize或行为。清单文件中的adjustResize|adjustPan设置将被忽略。
对我而言,这很难理解,因为我自己编写了一些(组合)视图,其中包含了RelativeLayoutScrollViews等。
如果我只是避免使用RelativeLayouts或其他任何非NestedScrollView处理视图滚动的方式,它就会按照我所希望的方式执行。
但我认为,在CoordinatorLayout中存在一个错误或我们需要更好地理解的行为,以避免出现这种问题...
也许对某些人来说这是有用的。
我的设置如下:
  • support/design/appcompat : 22.2.1
  • 无需layout_gravity

那么你的意思是在AppBarLayout下只使用NestedScrollView? - filthy_wizard
1
如果可以的话,请升级到支持库的新版本。我现在已经升级到23.x,不再遇到任何问题了。除了锚点重力必须设置为视图而不是协调布局本身之外。 - SukiSoft
我曾经遇到过类似的问题,解决方法的要点是将NestedScrollView作为CoordinatorLayout的子项。我没有通过使用adjustPan来解决这个问题,但至少默认行为(我相信是adjustResize)开始正常工作了。感谢您指引我正确的方向! - pajton

6
不要将CoordinatorLayout用作根布局,如果在NestedScrollView中使用EditText视图。
<androidx.constraintlayout.widget.ConstraintLayout
  android:fitsSystemWindows="true">

  <androidx.coordinatorlayout.widget.CoordinatorLayout>
    <com.google.android.material.appbar.AppBarLayout>

      <com.google.android.material.appbar.CollapsingToolbarLayout/>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView>
    <Editext/>
    <androidx.core.widget.NestedScrollView/>

  </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

没有任何一个解决方案有效,但是你的有效了,非常感谢你提供的解决方案! - hetsgandhi

2

使用 app:layout_behavior="@string/appbar_scrolling_view_behavior" 属性时,adjustResize 无法正常工作。

请删除这一行:

app:layout_behavior="@string/appbar_scrolling_view_behavior" 

2
你好,我遇到了同样的问题,我只是添加了


        android:windowSoftInputMode="adjustPan"

在您活动的清单中,键盘会相应地移动视图


这对我有用,只是当键盘显示时工具栏被隐藏了。 - iwooli

1
KeyboardUtil 中缩短的版本,将其添加到您的活动中:
getWindow().getDecorView().getViewTreeObserver()
        .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    public void onGlobalLayout() {
        Rect r = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
        rootView.setPadding(0, 0, 0,
                activity.getResources().getDisplayMetrics().heightPixels - r.bottom);
    }
});

我使用了这个监听器,虽然它修复了我们所有人都遇到的那个 bug,但它移除了包含“完成”按钮的我的片段视图底部的 60dp。如果我更改最后一行:activity.getResources().getDisplayMetrics().heightPixels - r.bottom,并将其替换为 0,我就可以恢复完整的视图,但是监听器就不能与编辑文本一起使用了。有没有一种方法可以编辑它,使它不会切掉我的视图底部,同时仍然可以与编辑文本一起使用? - AndroidDev123

0

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