【问题标题】:Add Footer to CoordinatorLayout将页脚添加到 CoordinatorLayout
【发布时间】:2016-04-30 23:00:31
【问题描述】:

我想向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 中,出现页脚,否则不显示。

如何让页脚可见?

【问题讨论】:

  • 请查看这个博客-javacodegeeks.com/2013/10/…希望这对你有帮助!!!
  • @AdarshYadav 不,这在 coordinatorlayout 中不起作用
  • 你想一直显示那个页脚吗?还是只是在 viewpager 中?
  • 一直是@LinX64.. 现在点击一下就可以了.. 但以后总会更好

标签: android xml android-coordinatorlayout


【解决方案1】:

你已经拥有了:

<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,这似乎不太好。

另外,也许您需要显示 ViewPagerNestedScrollView 以便将来滚动内容。(需要一个技巧,但您可以做到这一点)。

所以,我的建议是,您应该能够在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 来显示其他内容。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
  • 2016-03-10
相关资源
最近更新 更多