修复CoordinatorLayout中的底栏

Han*_*nzo 56 android android-layout android-toolbar android-coordinatorlayout

我有一个CoordinatorLayout包含AppBarLayoutFrameLayout包含片段的a.

其中一个片段包含一个TabLayout顶部,一个列表槽RecyclerView和底部一个"自制"工具栏.

AppBarLayout配置了app:layout_scrollFlags="scroll|enterAlways"

我的问题是当滚动时,两个"工具栏"都隐藏了,AppBarLayout和我底部的"自制"工具栏.这是当前的行为

在此输入图像描述

我想修复底部的"自制"工具栏以保持可见但我无法实现它.

这是片段Layout的XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <android.support.design.widget.TabLayout
        android:id="@+id/toolbarfilter"
        android:layout_width="match_parent"
        android:background="@color/azul_asde"
        app:tabMode="fixed"
        app:tabMaxWidth="0dp"
        android:elevation="4dp"
        app:tabIndicatorColor="@color/verde_pastel"
        android:layout_height="wrap_content"
        />

    <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    <android.support.v7.widget.RecyclerView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
    </android.support.v4.widget.SwipeRefreshLayout>

        <LinearLayout
        android:id="@+id/toolbarselection"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:background="@color/azul_asde"
        android:elevation="4dp"
        android:visibility="visible"
        >
        <ImageView
            android:id="@+id/delete"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_delete_white_24dp"
            android:tint="@color/gris_desactivado" />
        <ImageView
            android:id="@+id/select"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_bookmark_border_white_24dp"/>

        <ImageView
            android:id="@+id/send"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_send_white_24dp"
            android:tint="@color/gris_desactivado" />

    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

编辑1: 这个 问题似乎是同样的问题.

Vis*_*ave 7

试试这个布局:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/container">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_navigation"
        android:layout_alignParentTop="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        <android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_margin="10dp"
            android:layout_height="110dp"
            android:padding="15dp"
            card_view:cardElevation="2dp"
            card_view:cardCornerRadius="4dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Test" />

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


        <android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view1"
            android:layout_width="match_parent"
            android:layout_margin="10dp"
            android:layout_height="110dp"
            android:padding="15dp"
            card_view:cardElevation="2dp"
            card_view:cardCornerRadius="4dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Test" />

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

            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/card_view3"
                android:layout_width="match_parent"
                android:layout_margin="10dp"
                android:layout_height="110dp"
                android:padding="15dp"
                card_view:cardElevation="2dp"
                card_view:cardCornerRadius="4dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Test" />

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

            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/card_view4"
                android:layout_width="match_parent"
                android:layout_margin="10dp"
                android:layout_height="110dp"
                android:padding="15dp"
                card_view:cardElevation="2dp"
                card_view:cardCornerRadius="4dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Test" />

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

            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/card_view5"
                android:layout_width="match_parent"
                android:layout_margin="10dp"
                android:layout_height="110dp"
                android:padding="15dp"
                card_view:cardElevation="2dp"
                card_view:cardCornerRadius="4dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Test" />

            </android.support.v7.widget.CardView>
        </LinearLayout>
    </ScrollView>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:gravity="bottom">

        <android.support.design.widget.BottomNavigationView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_weight="1"
            android:foregroundGravity="bottom"
            android:background="@color/green"
            app:itemIconTint="@color/white"
            app:itemTextColor="@color/white"
            android:id="@+id/bottomnav"
            app:menu="@menu/main">


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

    </RelativeLayout>



</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

  • 对于未来的读者来说,在现代 Android 应用程序中强烈不鼓励使用relativelayout (2认同)

Ris*_*man 5

最好的解决方案是建立这种层次结构

<ConstraintLayout>
    <CoordinatorLayout>
        <AppBarLayout>
           <ToolBar>
           <TabLayout>
        </AppBarLayout>
        <Inlude>
           Put the Main Content here.
        </Include>
    </CoordinatorLayout>

 
   Other Widgets that you want to be placed at a fixed position.

</ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

不要忘记添加

app:layout_behavior="@string/appbar_scrolling_view_behavior"
Run Code Online (Sandbox Code Playgroud)

将上面的行添加到您包含的布局父 ViewGroup 中

让我们举一个我正在谈论的方法的例子

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/container"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:context=".activity.Dashboard">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:elevation="0dp"
            app:layout_constraintBottom_toTopOf="@+id/container"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/background"
                android:elevation="0dp"
                app:layout_scrollFlags="scroll|enterAlways|snap"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:titleTextColor="#000000" />

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:tabBackground="@color/background"
                app:tabIndicatorColor="@color/colorAccent"
                app:tabMode="scrollable"
                app:tabSelectedTextColor="@color/black"
                app:tabTextColor="@color/black" />

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

        <include
            android:id="@+id/include"
            layout="@layout/content_main" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <RelativeLayout
        android:id="@+id/btSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/_50sdp"
        android:visibility="gone"
        app:layout_constraintBottom_toTopOf="@id/container"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btReceive">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fbSend"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/_30sdp"
            android:background="@color/colorAccent"
            android:src="@drawable/ic_upload"
            app:rippleColor="@color/colorAccent" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/fbSend"
            android:layout_alignRight="@id/fbSend"
            android:layout_alignBottom="@id/fbSend"
            android:layout_marginBottom="@dimen/_3sdp"
            android:elevation="@dimen/_10sdp"
            android:gravity="center"
            android:text="SEND"
            android:textColor="@color/white"
            android:textSize="@dimen/_8sdp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/btReceive"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/_50sdp"
        android:visibility="gone"
        app:layout_constraintBottom_toTopOf="@id/container"
        app:layout_constraintEnd_toStartOf="@+id/btSend"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fbReceive"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/_30sdp"
            android:background="@color/colorAccent"
            android:src="@drawable/ic_download"
            app:rippleColor="@color/colorAccent" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/fbReceive"
            android:layout_alignRight="@id/fbReceive"
            android:layout_alignBottom="@id/fbReceive"
            android:layout_marginBottom="@dimen/_3sdp"
            android:elevation="@dimen/_10sdp"
            android:gravity="center"
            android:text="RECEIVE"
            android:textColor="@color/white"
            android:textSize="@dimen/_8sdp" />
    </RelativeLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_7sdp"
        android:background="@drawable/bottom_bar_corner"
        android:padding="@dimen/_5sdp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <Button
            android:layout_width="@dimen/_24sdp"
            android:layout_height="@dimen/_24sdp"
            android:background="@drawable/ic_upload_download"
            android:minHeight="0dp"
            android:paddingTop="@dimen/_6sdp"
            android:paddingBottom="@dimen/_6sdp"
            android:textSize="@dimen/_13sdp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好。让我们来看看结果

在此处输入图片说明


Tru*_*rry 1

当从属视图(, )中的滚动内容小于视图高度时,此行为基本上会SCROLL从 中删除滚动标志,即。当不需要滚动时。它还会覆盖偏移滚动视图,这通常由. 添加页脚时效果很好,即。按钮、滚动视图或 中,其中每个页面的内容长度可能不同。AppBarLayoutRecyclerViewNestedScrollViewAppBarLayout.ScrollingViewBehaviorViewPager

https://gist.github.com/MaciejKaras/02bff315f00b87d80467a470424f22c3

已在/sf/answers/26071/​​93634中回答