【问题标题】:Footer below list view fixed at the bottom of the screen列表视图下方的页脚固定在屏幕底部
【发布时间】:2013-03-15 07:20:03
【问题描述】:

我试图在列表视图下方添加页脚。

这就是我的做法。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical" >

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:divider="#BE6D79"
    android:dividerHeight="3dp" />

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:background="@color/darkDetna"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/contactBtn"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@drawable/listselector"
        android:paddingRight="0dip"
        android:text="Contact" />

    <Button
        android:id="@+id/faq"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@drawable/listselector"
        android:padding="0dip"
        android:text="Terms And Conditions" />

    <Button
        android:id="@+id/feedback"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@drawable/listselector"
        android:padding="0dip"
        android:text="Feedback  " />

    <Button
        android:id="@+id/fullSIte"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@drawable/listselector"
        android:padding="0dip"
        android:text="Full Site" />
</LinearLayout>
</LinearLayout>

这里

但问题是列表内容很小或者只有一个列表项,页脚将位于列表视图下方。

我希望将页脚固定在屏幕底部。

提前感谢您的时间和帮助。

【问题讨论】:

    标签: android android-layout listview android-listview


    【解决方案1】:

    更好地使用RelativeLayout

    解释实现:

    • 添加要在屏幕底部对齐的页脚视图android:layout_alignParentBottom="true"
    • 然后在页脚布局上方添加列表视图android:layout_above="@+id/LinearLayout1"
    • 让您的列表视图填满屏幕android:layout_width="fill_parent"android:layout_height="fill_parent"

    更改您的代码:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/white"
        android:orientation="vertical" >
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@+id/LinearLayout1"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_weight="1"
            android:divider="#BE6D79"
            android:dividerHeight="3dp" />
    
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/LinearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="30dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:background="@color/darkDetna"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/contactBtn"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_weight="1"
                android:background="@drawable/listselector"
                android:paddingRight="0dip"
                android:text="Contact" />
    
            <Button
                android:id="@+id/faq"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_weight="1"
                android:background="@drawable/listselector"
                android:padding="0dip"
                android:text="Terms And Conditions" />
    
            <Button
                android:id="@+id/feedback"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_weight="1"
                android:background="@drawable/listselector"
                android:padding="0dip"
                android:text="Feedback  " />
    
            <Button
                android:id="@+id/fullSIte"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_weight="1"
                android:background="@drawable/listselector"
                android:padding="0dip"
                android:text="Full Site" />
        </LinearLayout>
    
    </RelativeLayout>
    

    【讨论】:

    • 谢谢。我忘记将列表视图设置在底部对齐的视图上方。
    • @madlymad,如果我们有多个页脚视图怎么办?我们怎样才能把它们中的大部分固定下来?
    • @user5038993 任何您需要随列表滚动的内容都可以添加到列表中或作为列表的页脚。其他任何东西都可以成为对齐底部布局的一部分。如果您需要更复杂的事情,例如根据用户滚动显示/隐藏页脚,那么您需要为滚动侦听器添加自定义代码或查找库。
    【解决方案2】:

    您可以使用RelativeLayout 作为布局的根视图。

    【讨论】:

      【解决方案3】:

      您可以使用RelativeLayout 代替LinearLayout,然后使用android:layout_alignParentBottom="true" 将视图添加到RelativeLayout 的底部

         <?xml version="1.0" encoding="utf-8"?>
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="@color/white"
          android:orientation="vertical" >
      
      <ListView
          android:id="@android:id/list"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:divider="#BE6D79"
          android:dividerHeight="3dp" />
      
      <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="30dp"
          android:background="@color/darkDetna"
          android:orientation="horizontal"
          android:layout_alignParentBottom="true" >
      
          <Button
              android:id="@+id/contactBtn"
              android:layout_width="wrap_content"
              android:layout_height="30dp"
              android:layout_weight="1"
              android:background="@drawable/listselector"
              android:paddingRight="0dip"
              android:text="Contact" />
      
          <Button
              android:id="@+id/faq"
              android:layout_width="wrap_content"
              android:layout_height="30dp"
              android:layout_weight="1"
              android:background="@drawable/listselector"
              android:padding="0dip"
              android:text="Terms And Conditions" />
      
          <Button
              android:id="@+id/feedback"
              android:layout_width="wrap_content"
              android:layout_height="30dp"
              android:layout_weight="1"
              android:background="@drawable/listselector"
              android:padding="0dip"
              android:text="Feedback  " />
      
          <Button
              android:id="@+id/fullSIte"
              android:layout_width="wrap_content"
              android:layout_height="30dp"
              android:layout_weight="1"
              android:background="@drawable/listselector"
              android:padding="0dip"
              android:text="Full Site" />
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        如果你不想使用RelativeLayout,你可以在第二个线性布局上设置android:layout_gravity="bottom"

        【讨论】:

        • 其他解决方案都不错,但这个最适合当前设计。 +1
        猜你喜欢
        • 1970-01-01
        • 2011-10-25
        • 2019-01-25
        • 2013-04-29
        • 2011-03-06
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        相关资源
        最近更新 更多