带有锚定顶部和底部的RelativeLayout?

问题描述:

我想创建一个android的屏幕,有一个TableLayout锚定到顶部,然后滚动视图填充中间和TableLayout锚定到底部。我使用了一个使用alignParentTop/alignParentBottom的RelativeLayout,底部的TableLayout显示出来,但是最上面的一个消失了。带有锚定顶部和底部的RelativeLayout?

是否有可能在顶部和底部分别锚定一个可滚动区域填充中间的单独部分?想象一下顶部和底部的按钮栏,中间有可滚动的项目。这是我的布局是接近,但不太工作。我也尝试过使用TableLayout和3行,中间的一行设置为fill_parent和ScrollView,但无法使其工作。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout01" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TableLayout 
     android:id="@+id/CategoryTable01" 
     android:stretchColumns="*" 
     android:background="#000000" 
     android:layout_height="80px" 
     android:layout_alignParentTop="true" 
     android:layout_width="fill_parent"> 
     <TableRow 
      android:id="@+id/tr01" 
      android:layout_height="fill_parent" 
      android:layout_width="wrap_content" 
      android:padding="10px" 
      android:layout_gravity="center_vertical|center_horizontal"> 
      <TextView android:text="TOP1" android:textColor="#0000FF" android:layout_height="fill_parent" android:layout_gravity="fill_vertical" android:textStyle="bold" /> 
      <TextView android:text="TOP2" android:textColor="#CCCCCC" android:layout_height="fill_parent" android:layout_gravity="fill_vertical" android:textStyle="bold" /> 
     </TableRow> 
    </TableLayout> 
<ScrollView android:id="@+id/ScrollView01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout02" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TableLayout 
     android:id="@+id/TableLayout01" 
     android:stretchColumns="*" 
     android:background="@drawable/gradient" 
     android:layout_height="wrap_content" 
     android:paddingLeft="20px" 
     android:layout_width="fill_parent"> 
     <TableRow 
      android:id="@+id/TableRow01" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_gravity="center_vertical|center_horizontal"> 
      <TextView android:textColor="#FFFFFF" 
       android:textStyle="bold" 
       android:textSize="12dip" 
       android:text="Sample Line 1"></TextView> 
     </TableRow> 
     <TableRow 
      android:id="@+id/TableRow02" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_gravity="center_vertical|center_horizontal"> 
      <TextView android:textColor="#FFFFFF" 
       android:textSize="11dip" 
       android:text="Sample Line 2"></TextView> 
     </TableRow> 
     <TableRow 
      android:id="@+id/TableRow03" 
      android:layout_height="600px" 
      android:layout_width="wrap_content" 
      android:layout_gravity="center_vertical|center_horizontal"> 
      <TextView android:textColor="#FFFF00" 
       android:textSize="11dip" 
       android:text="Sample Line 3"></TextView> 
     </TableRow> 
    </TableLayout> 
    </LinearLayout> 
    </ScrollView> 
    <TableLayout 
     android:id="@+id/CategoryTable02" 
     android:stretchColumns="*" 
     android:background="#000000" 
     android:layout_height="80px" 
     android:layout_alignParentBottom="true" 
     android:layout_width="fill_parent"> 
     <TableRow 
      android:id="@+id/tr02" 
      android:layout_height="fill_parent" 
      android:layout_width="wrap_content" 
      android:padding="10px" 
      android:layout_gravity="center_vertical|center_horizontal"> 
      <TextView android:text="ONE" android:textColor="#0000FF" android:layout_height="fill_parent" android:layout_gravity="fill_vertical" android:textStyle="bold" /> 
      <TextView android:text="TWO" android:textColor="#CCCCCC" android:layout_height="fill_parent" android:layout_gravity="fill_vertical" android:textStyle="bold" /> 
      <TextView android:text="THREE" android:textColor="#CCCCCC" android:layout_height="fill_parent" android:layout_gravity="fill_vertical" android:textStyle="bold" /> 
      <TextView android:text="FOUR" android:textColor="#CCCCCC" android:layout_height="fill_parent" android:layout_gravity="fill_vertical" android:textStyle="bold" /> 
     </TableRow> 
    </TableLayout> 
</RelativeLayout> 

感谢您的任何建议或帮助。

迈克尔

确实有可能。这就是我找到了我的应用程序的一个工作(我切出像IDS一堆不相干的东西):

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

    <!--this is the action bar at the top of the screen)--> 
    <fragment 
      android:layout_width="match_parent" 
      android:layout_height="40dip" /> 

    <!-- This is the list view in the middle --> 
    <ListView 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:layout_height="0dip"> 
    </ListView> 

    <!-- This is a bunch of buttons at the bottom --> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="5dip" 
     android:gravity="bottom"> 
    </RelativeLayout> 

</LinearLayout> 

的关键是你的中间列表视图设置为高度0dip,让layout_weight接管 - layout_weight与HTML中的百分比宽度/高度相当 - 如果你有4个不同的元素,权重为0.25,那么它们将占用屏幕的四分之一。所以重量1.0会采取任何可能的。

+0

非常酷,非常感谢! – user1025005

+0

非常酷,非常感谢你! layout_weight的解释非常有用,我想我试图让layout_height =“fill_parent”扮演这个角色。 我将切换到TableLayout,因为我的项目不喜欢片段,我认为是因为目标API级别。我没有真正使用过片段,它看起来像API级别11中介绍的东西?我对Android开发还有点新意,所以我并不总是确定哪些元素尚未使用。例如,我有ScrollView - > LinearLayout - > TableLayout,所以我可以滚动,但似乎矫枉过正,如果ListView相同。 – user1025005