【问题标题】:fix linearlayout at the bottom as a footer in android将底部的线性布局修复为android中的页脚
【发布时间】:2013-06-18 09:10:35
【问题描述】:

我已经做了一个简单的布局xml文件。我一直在一点一点地解决我的问题。我正在使用scrollview、linearlayout 和tableview。我有一个顶栏,它被锁定在顶部。它从不移动。在中间,我有一个滚动视图,我需要将最后一个线性布局作为页脚放在底部,如果用户向下滚动,我不希望它消失。我想把它锁在那里。

这是我的代码

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

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

         <TableRow
        android:background="@drawable/gradient"
        android:gravity="center">

            .......

         </TableRow>

   </TableLayout>

    <ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/sw_layout"
android:orientation="vertical">

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

    <TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#EEEEEE" >

        <TableRow
        android:id="@+id/tableRowDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

            ..........

            </TableRow>

        </TableLayout>

        /*****************************************/
        /* I want this to be footer at the bottom*/
        /*****************************************/
        <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_date"
            android:layout_height="35dp"
            android:padding="5dp"
            android:drawableLeft="@drawable/calendar"
            android:text="Tarih" />

            <Button
            android:id="@+id/btn_converter"
            android:layout_height="35dp"
            android:padding="5dp"
            android:drawableLeft="@drawable/calendar"
            android:text="Hesap" />
          </LinearLayout>
        /*****************************************/
        /* I want this to be footer at the bottom*/
        /*****************************************/

       </LinearLayout>
     </ScrollView>  
 </LinearLayout>

如何实现将页脚锁定在底部?

【问题讨论】:

  • 您可以使用线性布局制作另一个 xml 文件并将其包含在您的 xml 代码中
  • 但我该怎么做呢?
  • 使用 relativeLayout 并在页脚布局中添加标签:android:layout_alignParentBottom="true";对于您的 ScrollView ,您应该将其放在页脚布局上方(android:layout_above="@+id/idOfYourFooterLayout"
  • 看下面我添加了答案
  • @ayilmaz :请参阅下面的答案

标签: java android android-linearlayout android-scrollview


【解决方案1】:

您应该使用RelativeLayout 并在页脚布局中添加标签:android:layout_alignParentBottom="true";对于您的 ScrollView ,您应该将其放在页脚布局( android:layout_above="@+id/idOfYourFooterLayout" )上方

试试这个:

<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="wrap_content"
   android:orientation="vertical">

  <ScrollView android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:id="@+id/sw_layout"
       android:layout_above="@+id/footer"
       android:orientation="vertical">
//your UI...
  </ScrollView>

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

      <Button
          android:id="@+id/btn_date"
          android:layout_height="35dp"
          android:padding="5dp"
          android:drawableLeft="@drawable/calendar"
          android:text="Tarih" />

      <Button
          android:id="@+id/btn_converter"
          android:layout_height="35dp"
          android:padding="5dp"
          android:drawableLeft="@drawable/calendar"
          android:text="Hesap" />
   </LinearLayout>
</RelativeLayout>

【讨论】:

  • 如果页脚应该在 ScrollView 内怎么办?
  • @FranciscoCorralesMorales :只需在 ScrollView 中使用 RelativeLayout 作为根布局。并将android:layout_alignParentBottom="true"添加到您希望它成为UI页脚的视图中。
【解决方案2】:

您可以设置页眉、页脚和ScrollViewlayout_height="0dp" 并定义layout_weight。只需使用这些值,直到找出最有效的值。生成的页眉和页脚高度会随着屏幕尺寸动态变化。

值 1、5、1 表示第一个元素占用父级LinearLayout 中可用高度的 1/7,第二个 5/7 和最后一个 1/7。

试试

<!-- HEADER -->
<TableLayout
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:layout_weight="1" >

<!-- BODY -->
<ScrollView
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:id="@+id/sw_layout"
   android:orientation="vertical"
   android:layout_weight="5">

<!-- FOOTER -->
<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:orientation="vertical"
   android:layout_weight="1" >

【讨论】:

    【解决方案3】:

    使用父Layout作为RelativeLayout并将这个属性android:layout_alignParentBottom="true"添加到footer layout中,将LinearLayout(footer layout)放到scrollview之外。像这样

    <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="wrap_content"
       android:orientation="vertical">
    
       <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"  //// add this property
          android:orientation="vertical" >
    
          <Button
              android:id="@+id/btn_date"
              android:layout_height="35dp"
              android:padding="5dp"
              android:drawableLeft="@drawable/calendar"
              android:text="Tarih" />
    
          <Button
              android:id="@+id/btn_converter"
              android:layout_height="35dp"
              android:padding="5dp"
              android:drawableLeft="@drawable/calendar"
              android:text="Hesap" />
       </LinearLayout>
    

    【讨论】:

      【解决方案4】:

      像这样使用一个xml->>

      <LinearLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
      
      
      </LinearLayout>
      

      并将其包含为

      LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical">
      
        ...
      
        <!-- Overridden attributes never work. Nor do attributes like
             the red background, which is specified here. -->
        <include
            android:id="@+id/buttons_override"
            android:background="#ff0000"
            android:layout_width="fill_parent"
            layout="@layout/buttons"/>
      
      </LinearLayout>
      

      【讨论】:

        【解决方案5】:

        使用相对布局作为父布局,然后添加三个子视图,如下所示

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" >
            <View android:id="@+id/top_view"
             android:alignParentTop="true">  // Your view that as to be in Top 
            </View> 
        
             <ScrollView android:layout_below ="@id/top_view" > //scroll view in middle below topview
             </ScrollView>
        
            <View android:alignParentBotton="true">  //Your expected view in bottom
            </View>
        
        </RelativeLayout>
        

        【讨论】:

          【解决方案6】:

          在我的例子中,我在 Oncreate 函数和布局的顶部添加了这些代码行

          如下图

          代码在这里:

              getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-01-29
            • 1970-01-01
            • 2012-03-27
            • 2013-11-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多