带可滚动工具栏

时间:2017-02-28 23:32:54

标签: android android-layout android-fragments android-toolbar android-coordinatorlayout

我在页面顶部有一个带有CoordinatorLayout和工具栏的Activity。我以编程方式添加ListFragments。在Fragment中我也有一个FloatingActionButton,因为FAB对于每个ListFragment都是不同的。

我添加到工具栏等支持可滚动工具栏 - 如果我将ListView滚动到顶部我希望工具栏淡出。但是 - 事实并非如此。如果我单击(但不释放)它,我只能通过触摸或在模拟器中通过鼠标移动工具栏。下一个问题是。如果我添加layout_scrollFlags和layout_behavior,则FAB和ListView位于NavigationBar后面的底部。如果我删除它并将一个android:paddingTop =“?attr / actionBarSize”添加到FrameLayout(只有那时我可以看到工具栏)底部一切都很好 - FAB和ListView结束于NavigationBar的顶部 - 但是当然工具栏不滚动。在没有NavigationBar的手机上,这些东西都在显示器的末尾 - 我只能看到FAB的三分之一和ListView的最后一行。

那么 - 如何让工具栏可滚动,同时FAB和ListView不在NavigationBar的后面?

activity_main.xml中:

<android.support.v4.widget.DrawerLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view_left"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
     />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view_right"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:fitsSystemWindows="true"
    />
</android.support.v4.widget.DrawerLayout>

app_bar_main.xml:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
android:id="@+id/app_bar_main"
tools:context=".frontend.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    app:elevation="0dp">

    <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"
        app:layout_scrollFlags="scroll|enterAlways">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageButton
                android:id="@+id/menuLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:src="@drawable/ic_menu_white_24dp"
                android:tint="@color/colorPrimary" />
            <TextView
                android:id="@+id/header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Example application"
                android:textSize="20dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:textAlignment="center"
                android:layout_toRightOf="@+id/menuLeft"
                android:layout_toLeftOf="@+id/menuRight"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textColor="@color/headerTextColorPrimary" />
            <ImageButton
                android:id="@+id/menuRight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:src="@drawable/ic_menu_white_24dp"
                android:tint="@color/colorPrimary" />
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>

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

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <ImageView
        android:layout_height="match_parent"
        android:src="@drawable/blank_screen"
        android:scaleType = "center"
        android:layout_width="match_parent" />
</FrameLayout>

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

list_fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".frontend.ListFragment"
android:fitsSystemWindows="false">

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    tools:listitem="@android:layout/simple_expandable_list_item_1"
    android:choiceMode="singleChoice"
    android:clickable="true"
    android:divider="#00000000"
    android:dividerHeight="1dp"
    android:background="@color/backgroundListView"/>

<TextView
    android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_centerVertical="true"
    android:text="@string/emptyTaskList"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:backgroundTint="@color/colorFloatingButton"
    android:src="@drawable/add"
    app:layout_anchorGravity="bottom|end"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    app:borderWidth="0dp"/>

</RelativeLayout>

0 个答案:

没有答案