如何将页脚垂直方向固定在屏幕底部?

时间:2017-09-21 09:44:38

标签: android android-layout scrollview android-linearlayout

我有标题,菜单和页脚的活动。在屏幕的垂直方向上,页脚位于ScrollView的底部,但它应固定在屏幕的底部。 ScrollView和页脚之间必须有垂直方向的空闲空间。

如何将页脚固定在垂直方向的屏幕底部?

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context="com.test.test.MainMenuActivity">

<LinearLayout
    android:background="@color/colorMainWhite"
    style="@style/root_layout">

    <!--- Header-->
    <include layout="@layout/content_header"/>
    <include layout="@layout/content_subheader"/>


    <!--- Menu with scroll-->
    <ScrollView
        android:id="@+id/myview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">
            <LinearLayout
                android:id="@+id/merchantInfoLayout"
                android:paddingTop="@dimen/size_15px"
                android:orientation="vertical"
                style="@style/los_lo_merchant_info_item">
                <TextView
                    android:id="@+id/tvNL1"
                    tools:text="test"
                    style="@style/tvs_lo_text_32_bold_ellipsized"/>
                <TextView
                    android:id="@+id/tvNL2"
                    tools:text="test"
                    style="@style/tvs_lo_text_26_ellipsized"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical">
                <LinearLayout
                    android:id="@+id/button1"
                    style="@style/los_lo_mainmenu_item">
                    <ImageView
                        style="@style/icon_default_size_center"
                        android:src="@drawable/icon_1"/>
                    <TextView
                        style="@style/tvs_lo_text_mainmenu_button"
                        android:text="@string/title_item_1"/>
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/button2"
                    style="@style/los_lo_mainmenu_item">
                    <ImageView
                        style="@style/icon_default_size_center"
                        android:src="@drawable/icon_2"/>
                    <TextView
                        style="@style/tvs_lo_text_mainmenu_button"
                        android:text="@string/title_item_2"/>
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/button3"
                    style="@style/los_lo_mainmenu_item">
                    <ImageView
                        style="@style/icon_default_size_center"
                        android:src="@drawable/icon_3"/>
                    <TextView
                        style="@style/tvs_lo_text_mainmenu_button"
                        android:text="@string/title_item_3"/>
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/button4"
                    style="@style/los_lo_mainmenu_item">
                    <ImageView
                        style="@style/icon_default_size_center"
                        android:src="@drawable/icon_4"/>
                    <TextView
                        style="@style/tvs_lo_text_mainmenu_button"
                        android:text="@string/title_item_4"/>
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/button5"
                    style="@style/los_lo_mainmenu_item">
                    <ImageView
                        style="@style/icon_default_size_center"
                        android:src="@drawable/icon_5"/>
                    <TextView
                        style="@style/tvs_lo_text_mainmenu_button"
                        android:text="@string/title_item_5"/>
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/button6"
                    style="@style/los_lo_mainmenu_item">
                    <ImageView
                        style="@style/icon_default_size_center"
                        android:src="@drawable/icon_6"/>
                    <TextView
                        style="@style/tvs_lo_text_mainmenu_button"
                        android:text="@string/title_item_6"/>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

     <!--- Footer-->
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:gravity="bottom">
        <LinearLayout
            android:paddingTop="@dimen/size_20px"
            android:paddingBottom="@dimen/size_20px"
            android:background="@color/colorBgLightGrey"
            android:layout_width="match_parent"
            android:gravity="center_horizontal"
            android:layout_height="wrap_content">
            <ImageView
                android:src="@drawable/icon_logos_ips"
                style="@style/ips_icons_style"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
 </android.support.design.widget.CoordinatorLayout>

2 个答案:

答案 0 :(得分:1)

I have made some edits to your original layout. The footer will appear at 

现在底部并且不与scrollview的内容重叠。 It will appear like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true">

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

    <!--- Header-->
    <!--<include layout="@layout/header_layout" />-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/cardview_dark_background"
        android:orientation="vertical"
        android:id="@+id/ll_top">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"                  
        android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />
    </LinearLayout>

    <!--- Menu with scroll-->
    <ScrollView
        android:id="@+id/myview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@+id/ll_top"
        android:layout_above="@+id/footer"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/merchantInfoLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingTop="5dp">

                <TextView
                    android:id="@+id/tvNL1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    tools:text="test" />

                <TextView
                    android:id="@+id/tvNL2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    tools:text="test" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="title_item_1" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="title_item_2" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/button3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="title_item_3" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/button4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="title_item_4" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/button5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="title_item_5" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/button6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="title_item_6" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/button7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="title_item_6" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/button8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="title_item_6" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <!--- Footer-->
    <!--<include layout="@layout/footer_layout" />-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/footer"
        android:layout_alignParentBottom="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:gravity="center_horizontal"
            android:paddingBottom="20px"
            android:paddingTop="20px">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

答案 1 :(得分:1)

您必须通过在其上设置android:layout_weight="1"来展开其中一个上方视图以填充剩余空间。这会将您的上一个视图推到底部。

检查this link

下面是你的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!--- Header-->
        <!--<include layout="@layout/header_layout" />-->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/cardview_dark_background"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />
        </LinearLayout>

        <!--- Menu with scroll-->
        <ScrollView
            android:id="@+id/myview"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/merchantInfoLayout"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:paddingTop="5dp">

                    <TextView
                        android:id="@+id/tvNL1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        tools:text="test" />

                    <TextView
                        android:id="@+id/tvNL2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        tools:text="test" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:orientation="vertical">

                    <LinearLayout
                        android:id="@+id/button1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="title_item_1" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/button2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="title_item_2" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/button3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="title_item_3" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/button4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="title_item_4" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/button5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="title_item_5" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/button6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="title_item_6" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/button7"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="title_item_6" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/button8"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="title_item_6" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
        </ScrollView>

        <!--- Footer-->
        <!--<include layout="@layout/footer_layout" />-->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorAccent"
                android:gravity="center_horizontal"
                android:paddingBottom="20px"
                android:paddingTop="20px">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_action_hardware_keyboard_arrow_left" />
            </LinearLayout>
        </LinearLayout>

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