如何使AppBarLayout坚持布局的顶部

时间:2018-04-10 00:11:17

标签: android drawerlayout android-appbarlayout

当软键盘出现时,我希望 AppBarLayout 能够粘在屏幕顶部,但它总是被推到屏幕外。

  • 活动有android:windowSoftInputMode="adjustPan"(也尝试将其设置为默认值)。

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

  • 我尝试在AppatorLayout中包装AppBarLayout并设置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 个答案:

答案 0 :(得分:0)

使用以下代码:

1.首先为你的AppBarLayout设置id android:id =&#34; @ + id / text1&#34; 然后将你的FrameLayout设置为AppBarlayout android:layout_below =&#34 ; @ + ID /文本1&#34;

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