如何在带有ViewPager的CoordinatorLayout中添加底部视图?

35

我想给一个带有view pagerCoordinator布局添加底部视图,底部视图将位于由view pager加载的fragment上方,且独立于其之上。

我添加了一个linear layout.

layout_gravity = "bottom"

但是底部视图线性布局根本没有显示。

以下是我活动xml布局。

<android.support.design.widget.CoordinatorLayout

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.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/maintoolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    <android.support.design.widget.TabLayout
        android:id="@+id/maintabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill" />
</android.support.design.widget.AppBarLayout>





<android.support.v4.view.ViewPager
    android:id="@+id/mainviewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />


<LinearLayout
    android:id="@+id/completeBottomView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ProgressBar
        android:id="@+id/progressBarBottomView"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:indeterminate="false"
        android:visibility="gone"
        android:max="100"
        android:progress="1"/>

    <HorizontalScrollView
        android:id="@+id/limiter_scroller"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|start"
        android:background="#FF3399"
        >
        <LinearLayout
            android:id="@+id/limiter_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:onClick="clickFromBottomView"/>
    </HorizontalScrollView>

</LinearLayout>

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

3
将completeBottomView添加到FrameLayout中尝试。 - Dhaval Parmar
3个回答

54

正如@Dhawal在评论中指出的那样... 解决方案是将LinearLayout completeBottomView用带有android:layout_gravity="bottom"FrameLayout包装起来。


这解决了我的问题。我有一个EditText锚定在底部,当它获得焦点时需要出现在键盘上方。谢谢。 - filthy_wizard
12
直接将android:layout_gravity="bottom"添加到你的LinearLayout(completeBottomView)中应该也可以。我认为额外的FrameLayout是不必要的。 - O'Kamiye
8
这是否意味着底部视图的内容将会覆盖其他内容? - android developer
1
有什么解决方案吗?底部视图遮盖了我的viewPager的一部分内容。 - saintjab
如果您需要具有“fitsSystemWindows”的半透明栏,则这不是一个特别好的解决方案。 - Mihae Kheel

7
Android CoordinatorLayout底部布局行为。
activity_bottom.xml
<android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimaryDark"
            app:layout_scrollFlags="scroll|enterAlways"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#C0C0C0"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="bottom"            
        app:layout_behavior="com.utils.behaviour.BottomLayoutBehavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#007432"
            android:gravity="center"
            android:text="Footer View"
            android:textColor="@android:color/white"
            android:textSize="25sp" />
    </LinearLayout>

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

BottomLayoutBehavior.kt

package com.utils.behaviour

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import kotlin.math.max
import kotlin.math.min

class BottomLayoutBehavior<V : View>(context: Context?, attrs: AttributeSet?) :
    CoordinatorLayout.Behavior<V>(context, attrs) {

    override fun onStartNestedScroll(
        coordinatorLayout: CoordinatorLayout, child: V,
        directTargetChild: View, target: View, axes: Int, type: Int
    ): Boolean {
        return axes == ViewCompat.SCROLL_AXIS_VERTICAL
    }

    override fun onNestedPreScroll(
        coordinatorLayout: CoordinatorLayout, child: V,
        target: View, dx: Int, dy: Int, consumed: IntArray, type: Int
    ) {
        child.translationY = max(
            0f,
            min(child.height.toFloat(), child.translationY + dy)
        )
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type)
    }
}

0
这段代码对我很有用。 将android.support.v7.widget.Toolbar的app:layout_scrollFlags="scroll|enterAlways"属性更改为app:layout_scrollFlags="scroll"。 更多细节在这里

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