相对布局中如何设置上下固定的锚点?

3
我试图创建一个Android屏幕,其中包含一个锚定在顶部的TableLayout,然后是填充中间的ScrollView和一个锚定在底部的TableLayout。我使用了RelativeLayout,并使用alignParentTop/alignParentBottom,但是底部的TableLayout会出现,而顶部的则会消失。
是否可能拥有分别固定在顶部和底部的不同部分,以滚动区域填充中间?想象一下在顶部和底部都有按钮栏,中间有可滚动的项。这是我的布局,它很接近但并不完全适用。我还尝试过使用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>

感谢任何建议或帮助。

迈克尔


你可以尝试使用:layout_below、layout_above。 - Hoàng Nguyễn
1个回答

6

的确是可以的。这就是我在我的一个应用程序中实现它的方法(我删掉了一堆无关的东西,比如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>

关键是将您的中间listview设置为高度为0dip,让layout_weight接管 - layout_weight有点类似于HTML中的百分比宽度/高度 - 如果您有4个不同的元素,其权重为0.25,则它们每个将占据屏幕的四分之一。因此,权重1.0占用尽可能多的空间。


非常酷,非常感谢!layout_weight的解释非常有用,我想我一直在尝试让layout_height="fill_parent" 扮演那个角色。我将<fragment />切换为TableLayout,因为我的项目不喜欢fragment,我认为这是由于目标API级别的原因。我还没有真正使用过fragments,它看起来像是在API级别11中引入的东西?我对Android开发还有点新,所以我还不确定应该使用哪些元素。例如,我有ScrollView->LinearLayout->TableLayout,这样我就可以滚动,但如果ListView可以做同样的事情,那么似乎有些过度设计了。 - user1025005

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接