如何使AppBarLayout固定在布局顶部

3
我希望AppBarLayout在软键盘出现时能够固定在屏幕顶部,但它总是被推到屏幕外。
  • 活动有android:windowSoftInputMode="adjustPan"(尝试将其设置为默认)。

  • 我已经尝试在AppBarLayout上设置app:layout_scrollFlags="scroll|enterAlways|snap"(以及此属性的各种配置)。

  • 我已经尝试将AppBarLayout包装在CoordinatorLayout中,并设置android:layout_alignParentTop="true"app:layout_constraintStart_toStartOf="parent"app:layout_constraintBottom_toBottomOf="parent"

我该如何获得所需的效果?

Basic layout

Soft keyboard appears and AppBarLayout dissappears

This is what I want to happen instead––Soft keyboard appears and AppBarLayout sticks to top of screen

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_green_light"
    android:fitsSystemWindows="true"
    tools:openDrawer="end">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/colorSecondary"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:itemBackground="@drawable/drawer_menu_highlighted_item_background_color"
        app:itemIconTint="@drawable/drawer_menu_highlighted_item_text_color"
        app:itemTextAppearance="@style/GlobalMenuText"
        app:itemTextColor="@drawable/drawer_menu_highlighted_item_text_color"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>
1个回答

1
请使用以下代码:

1.首先为您的AppBarLayout设置id android:id =“@ + id / text1”,然后在AppBarlayout下方设置FrameLayout android:layout_below =“@ + id / text1”

<RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text1"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />


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




        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/text1"/>


        </RelativeLayout>

我添加了id,将layout_below规则添加到FrameLayout中,并尝试过使用和不使用RelativeLayout进行包装,但没有效果。感谢您的建议。 - Miguel Valencia

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