Android - 如何添加简单的页脚?

时间:2012-05-14 00:05:48

标签: android android-layout

我一直在浏览不同的帖子,他们都试图留在屏幕上。

但我希望页脚出现在每个页面上。我的一些页面没有滚动,但有些页面没有。每当有滚动时,我希望页脚出现在滚动下方。怎么办?

例如,如果我有这个页面:

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

<ScrollView
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >    

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

<TextView
    android:id="@+id/page_exlain"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Some text that either extends far down or is pretty short."
    android:layout_marginTop ="20dp" 
    />          


</LinearLayout>

</ScrollView>

</LinearLayout>

添加页脚的好方法是什么,不一定会出现在首页?

谢谢!

1 个答案:

答案 0 :(得分:4)

我这样做的方法是在父布局中设置两个线性布局。第一个是我称之为内容区域,权重为1,这意味着它将尝试从父视图中获取尽可能多的空间。另一方面,页脚布局没有重量,因此即使其他视图(内容区域)为空,也会保持与内容相匹配的高度。

您可以在此布局的scrollview部分内添加content或任何其他类型的布局,而不会破坏这两个元素的处置,也无需担心页脚的位置,因为它将始终位于屏幕的底部。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/main"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical" >
<LinearLayout
                    android:id="@+id/content"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical" >
</LinearLayout>
<LinearLayout
                    android:id="@+id/footer"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >
</LinearLayout>

如果在前面的代码中添加了一些内容,那么您最终会得到类似的内容,请注意它非常简单。只要您了解适当的重量属性,您就没有问题根据您的需要进行修改。

enter image description here

您只需要将“内容”LinearLayout视为父内容,插入滚动视图或任何您需要的内容并忘记页脚。请注意,如果页脚是递归的,这意味着您将多次使用它,您可以直接将其加载到xml中而不将其复制到所有布局中

<include layout="@layout/footer" />

其中@layout/footer是layouts文件夹中的xml文件,其中包含要重用的页脚内容。这几乎与手动添加它相同,但不必在多个文件中维护它。

希望我有所帮助。

相关问题