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" 作为属性。
4

9 回答 9

17

这只是设计支持库中的另一个错误,似乎他们不打算很快修复它

已确认,但可能不会很快修复。adjustResize 有一些很好的用例,但是将 AppBarLayout 与 CollapsingToolbarLayout 一起使用并不是其中之一。

令人不安的是,在开发设计支持库时,没有人试图将 a 添加EditText到 a中。CoordinatorLayoutandroid:windowSoftInputMode="adjustResize"

于 2016-08-23T11:08:05.780 回答
16

尝试这个


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>
于 2016-03-31T01:12:17.020 回答
10

I'm using this for solving CoordinatorLayout bug

in onCreateDialog in BottomSheetFragment

KeyboardUtil(getActivity(), view);

or

For Activity use

new KeyboardUtil(this, findViewById(R.id.fragment_container));

by using this Util class

Java version: https://github.com/mikepenz/MaterialDrawer/blob/aa9136fb4f5b3a80460fe5f47213985026d20c88/library/src/main/java/com/mikepenz/materialdrawer/util/KeyboardUtil.java

Kotlin version: https://gist.github.com/agustinsivoplas/6f80233699aebebd512542503ea32682 Credit:Mikepenz,Agustinsivoplas

于 2017-02-17T09:23:21.207 回答
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>

我有同样的问题。我发现,如果我在 egNestedScrollView或a 中使用某些东西,FrameLayout我会假设它也处理 Scroll 本身,那么CoordinatorLayout它不会adjustResize/按照你想要的方式行事。Manifest 中的 adjustResize|adjustPan 设置被忽略。

对我来说,这很难弄清楚,因为我自己写了一些(组合)视图,其中包含RelativeLayoutorScrollViews等​​。

如果我只是避免使用RelativeLayoutsNestedScrollView处理视图滚动之外的任何其他事情,它的行为方式,至少我想要它。

但我认为,CoordinatorLayout我们都需要更好地理解其中的错误或行为以避免此类问题......

也许这对某人有用...

我的设置是:

  • 支持/设计/appcompat:22.2.1
  • 不需要 layout_gravity
于 2016-01-20T07:12:13.110 回答
6

如果您在 NestedScrollView 上使用 EditText 视图,请不要使用 CoordinatorLayout 作为根布局

<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>
于 2019-03-06T07:09:33.947 回答
2

嗨我有同样的问题,我只放

        android:windowSoftInputMode="adjustPan"

在相关活动的清单中,键盘现在相应地向上移动视图

于 2016-05-08T11:42:07.083 回答
2

adjustResize 不适用于

app:layout_behavior="@string/appbar_scrolling_view_behavior"

删除这一行:

app:layout_behavior="@string/appbar_scrolling_view_behavior" 
于 2018-06-14T12:03:24.383 回答
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);
    }
});
于 2018-09-11T14:55:48.573 回答
0

现在我正在使用 androidx.drawerlayout.widget.DrawerLayout 来解决它:

CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout、工具栏、NestedScrollView

这可能不是最好的主意,但它对我有用;)

我的项目中有一个示例:https ://github.com/maiconpintoabreu/Kupping-Mobile/blob/master/app/src/main/res/layout/activity_event_new.xml

于 2019-09-09T23:13:37.313 回答