【问题标题】:RelativeLayout with anchored top and bottom?具有锚定顶部和底部的RelativeLayout?
【发布时间】:2020-03-02 23:01:50
【问题描述】:

我正在尝试创建一个 Android 屏幕,它的 TableLayout 锚定在顶部,然后滚动视图填充中间,TableLayout 锚定在底部。我使用了带有 alignParentTop/alignParentBottom 的 RelativeLayout,底部的 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

标签: android android-layout


【解决方案1】:

确实有可能。这就是我让它在我的一个应用程序中工作的方式(我已经删除了一堆不相关的东西,比如 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 占据了它所能做的一切。

【讨论】:

  • 非常酷,非常感谢! layout_weight 的解释非常有用,我想我试图让 layout_height="fill_parent" 扮演那个角色。我将 切换到 TableLayout 因为我的项目不喜欢片段,我认为是因为目标 api 级别。我还没有真正使用过片段,它看起来像是在 API 级别 11 中引入的东西?我对 android 开发还是有点陌生​​,所以我还不总是确定要使用哪些元素。例如,我有 ScrollView -> LinearLayout -> TableLayout 所以我可以滚动,但如果 ListView 这样做似乎有点过分了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-07
  • 2012-02-24
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多