在 Android 中将布局添加到屏幕底部

Adding layout to the bottom of screen in Android

我想添加这样的布局:

    <header layout>(Fixed)
         |
         |
     <ScrollView>
         |
         |
    <Footer Layout>(Fixed)

这是我目前尝试过的方法:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >

    <ScrollView
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/footer" >

        <LinearLayout
            android:id="@+id/liMain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>   
</RelativeLayout>

您可以为要在 top/bottom 对齐的布局添加 android:layout_alignParentBottom="true"android:layout_alignParentTop="true"。分配这些 ID,然后在您的 ScrollView 上添加 android:layout_above="@id/layout_above"android:layout_below="@id/layout_below"

像这样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:id="@+id/header"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:textAppearanceLarge"
        android:text="Header"/>


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/header"
    android:layout_above="@+id/footer"/>
    <TextView
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_alignParentBottom="true"
        android:textAppearance="?android:textAppearanceLarge"
        android:text="Footer"/>

</RelativeLayout>

使用下面的代码来实现你需要的布局..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:fb="http://schemas.android.com/apk/res-auto"
  android:id="@+id/drawer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/white"
  android:orientation="vertical" >

<TextView
    android:id="@+id/tv_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="#123456"
    android:gravity="center"
    android:text="HEADER.." />

<ScrollView
    android:id="@+id/scorllview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/tv_footers"
    android:layout_below="@+id/tv_header" >

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="center"
            android:text="your data..." />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="center"
            android:text="your data..." />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="center"
            android:text="your data..." />
    </LinearLayout>
</ScrollView>

<TextView
    android:id="@+id/tv_footers"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#123456"
    android:gravity="center"
    android:text="FOOTER" />

</RelativeLayout>

此问题可能与 this

重复

记住上面的例子中缺少 header 部分,所以你可以这样声明

 <LinearLayout
   android:id="@+id/header"
    android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_alignParentTop="true"
   android:weightSum="1"
   android:gravity="center" >
   <Button
            android:id="@+id/button_show_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Header." />

     </LinearLayout>

Nd 在 Scrollview 中放一行

 android:layout_below="@id/header" >

试试这个,你也可以根据自己的喜好在页脚和页眉中添加按钮。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout   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" >
 <!-- Header -->
 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/ebook_header_height"
    android:background="#000000"
    android:orientation="horizontal" >
  <Button
        android:id="@id/btn_back"
        android:layout_width="@dimen/logout_btn_width"
        android:layout_height="@dimen/logout_btn_height"
        android:layout_marginLeft="@dimen/linear_side_margin"
        android:background="@drawable/back_btn"
        android:gravity="center"
        android:paddingLeft="@dimen/download_btn_padding"
        android:paddingRight="@dimen/download_btn_padding"
        android:text="@string/back"
        android:textColor="#ffffff"
        android:textSize="@dimen/download_btn_tsize"
        android:textStyle="bold" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="13"
        android:gravity="center"
        android:text="@string/verify_phone"
        android:textColor="#ffffff"
        android:textSize="@dimen/tabs_title_tsize"
        android:textStyle="bold" />
    <Button
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:layout_marginRight="@dimen/download_count_margin"
        android:background="@drawable/blank_right_btn"
        android:gravity="center" />
</LinearLayout>
<ScrollView
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/footer" >
<LinearLayout
android:id="@+id/liMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
 </LinearLayout>
</ScrollView>   
<!-- FOOTER -->
<LinearLayout
    android:id="@+id/footer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="#A6B2D0"
    android:gravity="center_vertical|center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="@dimen/padding_link"
    android:paddingTop="@dimen/padding_link" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/above_hyperlink"
        android:textColor="#444444"
        android:textSize="@dimen/tabs_title_tsize" />
    <TextView
        android:id="@+id/univ"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hyperlink"
        android:textColor="#444444"
        android:textSize="@dimen/tabs_title_tsize" />
 </LinearLayout>
</RelativeLayout>

您只需添加一行以将布局与底部对齐,例如android:layout_alignParentBottom="true"