android - 将页脚添加到 CoordinatorLayout

标签 android xml android-coordinatorlayout

我想向 CoordinatorLayout 添加一个页脚,其中包含一个 TabView + ViewPager

这是 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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="true"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.entree.entree.activity.StoreActivity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/appbar_padding_top"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/black"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_action_tray"
                    android:layout_gravity="right"
                    android:layout_marginRight="15dp"
                    android:scaleType="centerInside"/>

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_action_find"
                    android:layout_gravity="right"
                    android:scaleType="centerInside"
                    android:layout_marginRight="10dp"
                    />
            </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:background="@android:color/white"
                app:tabIndicatorColor="#00000000"
                style="@style/MyCustomTabLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

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

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


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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:visibility="gone"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_search"
        app:menu="@menu/activity_search_drawer" />
</android.support.v4.widget.DrawerLayout>

这是 Xml 内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/container"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.entree.entree.activity.StoreActivity$PlaceholderFragment">


<GridView
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:numColumns="1"
    android:stretchMode="columnWidth">

</GridView>

<LinearLayout
    android:id="@+id/llTray"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3"
    android:visibility="gone"
    android:gravity="center_vertical"
    android:layout_centerVertical="true"
    android:background="@color/divider"
    android:layout_alignParentBottom="true">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="left"
        android:gravity="left"
        android:layout_height="wrap_content">

        <ImageButton
            android:id="@+id/btnTray"
            android:src="@drawable/ic_action_tray_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_gravity="center_horizontal|center_vertical"
        android:gravity="center_horizontal|center_vertical"
        android:layout_weight="1"
        android:text="@string/view_tray" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:gravity="right"
        android:layout_marginRight="10dp"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tvTotalPrice"
            android:layout_width="wrap_content"
            android:textSize="20sp"
            android:textStyle="bold"
            android:text="@string/Rs"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</LinearLayout>

页脚在 xml 内容中。

如果我删除:

app:layout_behavior="@string/appbar_scrolling_view_behavior" 

在主 Xml View Pager 中,页脚出现,否则不显示。

如何让页脚可见?

最佳答案

您已经拥有:

<GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:numColumns="1"
        android:stretchMode="columnWidth" />

match_parent 用于 heightwidth 这似乎不太好。

此外,也许您将来需要使用 NestedScrollView 来显示 ViewPager 以滚动内容。(需要技巧,但您可以实现)。

因此,我的建议是,您应该能够通过像这样添加它来始终在 ViewPager 的下方显示该页脚:

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

        <!-- Footer aligned to bottom -->
        <RelativeLayout
            android:id="@+id/footer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="#FC0"
            android:gravity="center"
            app:layout_anchor="@id/main_content"
            app:layout_anchorGravity="bottom">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:text="Fixed Footer"
                android:textColor="#000"
                android:textSize="20sp" />

        </RelativeLayout>

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

这应该始终显示页脚(即使 ViewPager 正在折叠)。


或者如果你想在你的 ViewPager 中显示它:

你应该在你的内容中添加这个:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

也就是说,它会将内容放在AppbarLayout

下面

无论如何,您的内容问题来自 GridView 就像我说的那样,它将匹配所有内容并且您没有 ScrollView 甚至 NestedScrollView 显示其他内容。

关于android - 将页脚添加到 CoordinatorLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34978985/

相关文章:

android - 调用显示 ProgressDialog 的新 Activity 后隐藏键盘

android - 在模块中发现重复的类

android - Windows docker 容器中的 ADB 设备为空

xml - XSL 无法编译样式表

c# - 将 xml 提要中的日期字符串解析为 DateTime

java - 使用VTD-XML仅修改元素文本

android - 折叠工具栏-状态栏下的工具栏

Android Studio 突然无法解析符号 R

android - 后退按钮未显示在 CollapsingToolbarLayout 中

java - CoordinatorLayout 中的 CollapsingToolbarLayout 根本不起作用