2012-05-14 22 views
1

我一直浏览不同的帖子,他们都有脚注试图留在屏幕上。Android - 如何添加一个简单的页脚?

但我希望页脚出现在每一页上。我的一些页面没有滚动条,但有些可以。每当有滚动时,我都希望页脚出现在滚动条下方。如何做到这一点?

举例来说,如果我有这个页面:

<?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> 

什么是页脚补充一点,不一定出现倍以上的好办法?

谢谢!

回答

4

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

您可以在不打破这两个要素的配置,并无需担心页脚的位置,因为它总是会在底部此布局的content部分内添加scrollview或任何其他类型的布局屏幕。

<?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,就好像它是一个家长,插入scrollviews或任何你需要和忘掉页脚。请注意,如果页脚是递归的,这意味着你将要使用它多次,你可以在XML加载的情况下直接在其拷贝到你所有的布局

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

@layout/footer是一个XML文件在你的布局文件夹中包含要重复使用的页脚的内容。这与手动添加它几乎是一样的,但不需要在多个文件中维护它。

希望我有帮助。

+1

+1 ...根据您的应用程序结构,我喜欢将所有这些内容抽象为基本活动,并创建受保护的方法以将内容添加到通用“基本”布局文件。这样,所有将来的活动都可以像setContentView()那样轻松填充。 – Phix

相关问题